Return Function in Python

The function consists of a block of code that you can use anywhere, or the function is reusable, but sometimes you must perform some operation and then return the result from the function.

By default, after performing some operation, the function returns None, so in this tutorial, you will learn how to create a function that returns a value or result.

What is the Return Function in Python

The return function is a block of code that, when called, returns the result to the caller. So, the function that returns a value contains a return keyword followed by the result or expression that evaluates the value.

In simple words, the function contains a return statement that sends back the data from the function to the caller.

The syntax is given below.

def function_name(parameters):
    # computations or operations
    return result

The result after the keyword “return” can be any data type, including numbers, strings, lists, dictionaries, instances of classes, or the value of any expression. Here the “return” keyword is responsible for returning the result from the function.

Let’s create a small function. For example, suppose you have to find the square of any number. Then, you can create a function that returns the square of any number, as shown below in the code.

def get_square(x):
  return x**2

get_square(2)

When you run the above code, it returns the square of 2, which is 4 as a result.

Python Return Function Name

Let’s create a function with a return statement to return a result or value from the function. Suppose you have a business where you need to apply a sales tax to a product’s price. For that, you have to create a function that applies sales tax and returns the final price.

def calculate_tax(price, tax_rate):
    total_price = price + (price * tax_rate)
    return total_price

product_price = 100  # price in dollars
sales_tax_rate = 0.05  # 5% tax rate

final_price = calculate_tax(product_price, sales_tax_rate)
print(f"The final price, including tax, is: ${final_price}")
Python Return Function Name

Look at the output. The function calculate_tax() accepts the price and tax_rate, then returns the total_price including tax, which is $105.0, with the product price equal to 100 and the sale tax rate equal to 0.05.

READ:  How to Remove Character from String Python By Index

In the function, this line of code, the “return total_price” statement, sends back the total_price from the function, which is computed using the “price + (price * tax_rate)”

Here, you need to understand one thing: “What does return do in Python?” now you know what a return statement does; it just returns or sends back the result from the function to the caller.

One thing I want to tell you is that you can also get the Python function name. For example, you can get the function name that you have created above using the __name__ attribute of Python as shown below in the code

print(calculate_tax.__name__)
python get function name

When you call the __name__ attribute on the function, it returns the function’s name, as you can see in the above picture. The function’s name is calculate_tax.

Maybe you are thinking, what weird is this to get the function where you already need to know the name of the function to call the __name__ attribute on it? However, it is beneficial for debugging or logging purposes, where you might want to include the name of the function in log messages.

From the above different examples, I hope you understand how to create a function with a return statement to send back the result from the function.

Conclusion

In this Python tutorial, you learned about the return function in Python by specifying the return keyword within the function. You also learned how to get the name of any function using the __name__ attribute.

You may also like to read: