Python function 'return' statement - In two of our previous articles, we've learned about Python function definition & calls and Python namespace & scope. Just like these articles, this one is also related to Python as we are going to know about return values and the
returnstatement. I recommend you to go through the mentioned articles, to have a clearer idea about this article, as it is linked to them.
Lets start this discussion with the important takeaways from the earlier articles on Python function :
- A function can be defined using the
defkeyword, the name of the function, followed by a pair of parenthesis.
- A function can have parameters to store the value of the argument, when the function is called.
- Then follows indented block of statements, where the entire logic is incorporated.
- We can call a function with its name and the parenthesis like
- We can define arguments to send some information to the function.
- Any variables used inside a function are stored in its local namespace.
- We have global namespace where all the variable names declared in the program body (outside functions) reside.
- We have enclosed namespace when two or more functions are nested, the variable names used in enclosing functions are stored in enclosed namespace.
- The built-in namespace consists mainly of Python keywords, functions and exceptions.
- A name is searched in these namespaces in a specific order as per LEGB rule i.e. Local, Enclosed, Global and Built-in.
Return Values and
So far, we have used functions to perform some tasks and print the result, may it be a simple string or the table of a number or some power of some number, using the
power(base, exp)that calculates
expth power of
baseand another function
cubert(num)to calculate cube root of a number. We wish to calculate a number using
power()and pass it as an argument to
cubert()function. As we are printing out the result on the stdout, we cannot use it in another function, even if we had stored it in a variable (remember local namespace and global namespace?). So, we need to have a mechanism with which the result from one function can be used in the program, as and when needed. And we have
returnstatement is optional to use. When used, it comes out of the current function and returns to the position where it was called i.e. the caller. It also sends back a value to the caller, which then can be used anywhere in the program or by another function. The value which needs to be handed back (known as return value), is decided by the programmer and it is the argument to the
return object_name. When
returnis not used, the function returns
Noneby default. To demonstrate this, lets check the example of our
power()function created in the previous article.
Example 1 : If
returnis not used,
>>> def power(base, exp) : ... print str(base) + ' to the power of ' + str(exp) + ' is ' + str(base ** exp) ... >>> myResult = power(3, 4) 3 to the power of 4 is 81 >>> print myResult None
In above example, the function
power()just prints the result on the screen,
returnstatement not being used. Knowing that the
returnnot used, the default value
Noneis returned to the caller, which then gets assigned to global variable
Example 2 : Returning a value
>>> def power(base, exp) : ... print str(base) + ' to the power of ' + str(exp) + ' is ' + str(base ** exp) ... return base ** exp ... >>> myResult = power(3, 4) 3 to the power of 4 is 81 >>> print myResult 81
In above example, we actually used the
returnstatement, that returns the result of exponentiation operation to the caller. Thus,
myResultis assigned with the value returned by the
power()and the same is printed. Had we written any statements after
return, they would not have executed.
Example 3 : Using return values from another function
# Function fo calculate 'n'th power of a number >>> def power(base, exp) : ... return base ** exp ... # Function to add two numbers >>> def addition(a, b) : ... return a + b ... # Function that user power() and addition() to add squares to two numbers >>> def myFunction(num1, num2): ... sqr_num1 = power(num1, 2) ... sqr_num2 = power(num2, 2) ... result = addition(sqr_num1, sqr_num2) ... return result ... # Function call >>> myFunction(3, 4) 25 >>> myFunction(8, 6) 100
In this example, we have two functions
addition(), those return the results of exponentiation and addition operations respectively. The third function
myFunctionused these two functions to calculate sum of the squares of two numbers and return the result.
With this, we end our discussion on Python function return values and
returnstatement. In this article, we learned how the result of the operations done by a function can be used to be processed in the program using return values, which we cannot achieve using