Python return Statement | Explained

The return statement is used with the keyword “return” along with the return value to send back the result of a function to a caller. The return function has a “return” statement which will exit the function’s execution and send the resultant value back to the caller.

This write-up will provide an in-depth overview of the Python return statement. The following outcomes are discussed in this Python tutorial:

So let’s get started!

What is a return Statement in Python?

The “return” statement defines the value/expression returned by the function. When the “return” is called inside the function, the code program will exit, and the final value of the function will be returned. The syntax of the “return” statement in the function is shown below:

def function_name():
  code statements....
  .....
  .....
  return [expression/variable]

In the above syntax, the “return” statement is initialized within the function, and when the “return” statement executes, the program exits and none of the statements after the return will be executed.

Example 1: Simple return Statement

In the below example, the “return” statement gets the value of the “division operator” and shows it on the screen when the function is called in the program.

Code:

def Numbers():
  return 10/4

print(Numbers())

In the above code:

  • The function named “Numbers” is defined.
  • When the function is called, the return function executes its command and retrieves the result which returns the answer of the expression (10/4).
  • Finally, the function named “Numbers” is accessed, and the value of the function is printed on the screen using the “print()” function.

Output:

The output shows the value “2.5” after division.

Example 2: return Statement With Expression Value

The “return” statement also returns the value of the expression initialized inside the function. The value stored in the variable is shown on the output when the function is accessed in the program. Let’s understand it via the given below example:

Code:

#return with expression
def sum(a, b):
    output = a + b
    return output

result = sum(15, 44)
print('sum of a and b = ',result)

In the following code:

  • The function named “sum” is initialized in the program by taking two argument values “a” and”b”.
  • The two expressions are added and stored in a variable named “output”.
  • The “return” function shows the output value of the variable when the sum function is accessed.
  • The value of the expression “a” and”b” is passed inside the argument value and stored in a variable named “result”.
  •  When the variable “result” is called inside the “print()” function. The “return” function shows the expression result on the screen.

Output:

The output shows the result of two expressions.

Example 3: return Statement to Access Another Function

In Python, the function is very similar to any object such as int, list, etc. Like the object, we can return one function to another in the Python program using the “return” statement. In the example given below, the function is called inside another function using the “return” statement with the function name.

Code:

def first_function():
    return second_function

def second_function():
    print("istlinuxfoss-Python Guide")

x = first_function()
x()

In the mentioned code:

  • Two functions are initialized in the program named “first_function” and “second_function”.
  • The return statement is used inside the “first_function” to access the second function of the program.
  • In the second function, the string value is defined inside the parentheses of the “print()” function.
  • The function named “first_function” is assigned to a variable named “x”, which is used to access the first function.

Output:

The above output shows the result of “second_function”.

Example 4: return Statement With “if-else” Statement

The “return” statement can also be initialized inside the “if-else” block of a function. When the condition is satisfied, the “return” statement returns that value to a function and shows output on the screen. Let’s understand this via the following examples:

Code:

Name = "Alex"

def marks(student):
    if student == "John":
        return 50
    elif student == "David":
        return 25
    elif student == "Alex":
        return 75
    elif student == "Mary":
        return 65
    else:
        return 20

x = marks(Name)
print(x)

In the following code:

  • The string value “Alex” is defined in the variable named “name”.
  • The function named “marks” is defined, and the variable “student” is passed inside its parentheses.
  • The condition statement “if-else if” is used to check different string values. If the value of the string is matched with the “name” variable value, then the statement code executes, which is written inside the “if” and “elif”.
  •  The “else” statement executes when none of the conditions is true.
  • The “return” function is written in every condition and, when executed, shows the result on the screen.

Output:

The output shows the marks of executed condition.

That’s it from this guide!

Conclusion

Python’s “return” function has a “return” statement inside the function, and when executed, it sends back the value to the main program. This statement exits the function and executes the rest of the program code. The “return” statement returns the expression value, object value, and operator output value to the function. It can also be used to access the second function inside another function. This Python write-up provided a detailed guide on the Python return statement.

Over 500 Million Views on Our Blogs and YouTube! We’re inviting 10 businesses to partner with us and grow together. Click here to join us! ×