Thursday, 8 December 2016

Python Funtion : 'return' Statement

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 return statement. 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 :
  1. A function can be defined using the def keyword, the name of the function, followed by a pair of parenthesis.
  2. A function can have parameters to store the value of the argument, when the function is called.
  3. Then follows indented block of statements, where the entire logic is incorporated.
  4. We can call a function with its name and the parenthesis like myFunc().
  5. We can define arguments to send some information to the function.
  6. Any variables used inside a function are stored in its local namespace.
  7. We have global namespace where all the variable names declared in the program body (outside functions) reside.
  8. We have enclosed namespace when two or more functions are nested, the variable names used in enclosing functions are stored in enclosed namespace.
  9. The built-in namespace consists mainly of Python keywords, functions and exceptions.
  10. 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 return Statement

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 print statement. A print statement writes the output to the screen (stdout), which cannot be used for further processing. Say, if we have two functions power(base, exp) that calculates expth power of base and 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 return statement.

The return statement 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 statement, e.g. return object_name. When return is not used, the function returns None by default. To demonstrate this, lets check the example of our power() function created in the previous article.

Example 1 : If return is not used, None is returned.

>>> 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

In above example, the function power() just prints the result on the screen, return statement not being used. Knowing that the print statement doesn't return anything and return not used, the default value None is returned to the caller, which then gets assigned to global variable myResult and displayed.

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

In above example, we actually used the return statement, that returns the result of exponentiation operation to the caller. Thus, myResult is 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)
>>> myFunction(8, 6)

In this example, we have two functions power() and addition(), those return the results of exponentiation and addition operations respectively. The third function myFunction used 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 return statement. 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 print statement. We have already discussed on the basics of function arguments, we will be learning about them much more details, in the next article. Please share your opinions and views in the comment section below and stay tuned for more interesting articles on Python. Thank you!

This article is originally published at - Python Function : Return Values and 'return' Statement


Post a Comment