Return statement in Python Function

Return statement in Python Function

In this session, we will be discussing Return statement in Python Function. In our previous two sessions, we understood basics of functions and parameterized functions. Today we will see how we can make a function to evaluate some instruction and return the result back to the function call.

To make function return some result, we make use of Python Keyword ‘return’. There are 2 ways to use ‘return’ keyword in a function – using without expression/result and using with expression/result.

Using return without expression/result – in this case, when return is used without an expression, the control immediately moves out of the function body and goes to the place where the function is called. Remember that when we do not use return, in that situation also return is implicitly invoked when all function instructions are completed.

Example: In below example, we have used if condition to check if number is even and then return is called which immediately comes out of the function. In other case when number is odd, then also return is implicitly called at the end of function definition.

def function(num):
     print('Number is:',num)
     if(num%2==0):
         print("This number is even")
         return
     print("This number is odd")

function(5)

Using return with expression/result – here we use return keyword to return the result of some expression. Once return is invoked, the control immediately moves out of the function.

Examples of Return statement in Python Function:

1. Returning result from function – here we are multiplying parameter value with 5 and returning the result. Note that we need a variable to store the value of the result returned by function.

def function(num):
     return num * 5
pro=function(8)
print("Answer is:",pro)

2. Returning result from function without a variable – we are directly using the function call inside the print statement hence we don’t need a variable

def function(num):
     return num * 5
print("Answer is:",function(8))

3. What will happen when return statement does not send any result to the function call and we try to use the result.

def function(num):
     return 
print("Answer is:",function(8))

Here since return statement does not has any expression, the result printed on screen will be ‘None’.

4. Getting ‘None’ output from return – in below example, we returned True when number is even. But when number is odd, we didn’t return anything. So, when we try to print the result using odd number in function call, we will see ‘None’ printed.

def odd_even(num):
     if num%2==0:
         return True
print(odd_even(2))
print(odd_even(1))

5. Sending list as function argument – just like a scalar variable, we can send list also as the function argument. Here we are adding up the elements of the list

def sum_list(list):
     sum=0
     for num in list:
         sum+=num
     return sum
print(sum_list([1,2,3,4]))

6. Returning list as the result of the function – here we are sending list as an argument and sending another list as the result. The new list will have each element double of the actual list

def new_list(list):
   new_lst=[]
   for num in list:     
      new_lst.append(num*2) 
      return new_lst

print(new_list([1,2,3,4,5]))

7. Creating ‘n’ element list and returning as result – here we are passing a variable ‘n’ in function argument and then creating a list having ‘n’ elements starting from 0 till 1 less than ‘n’. In below example, n=5.

def create_list(num):
   list=[]
   for ele in range(num):     
       list.append(ele) 
       return list

print(create_list(5))

8. Using function variable outside function will give error – we cannot use function variable outside the function since this variable does not have scope outside function

def fun():
  var=5
fun()
print(var)
NameError: name 'x' is not defined

9. Variable created outside a function can be used inside a function because this variable has scope inside function

def fun():
     print(var)
var=5    
fun()

10. Using same name for variable outside function and inside function – here both var variables are different and function variable shadows the variable outside function. This means that variable existing outside the function has scope inside function only when function does not have variable of the same name.

def fun():
     var=6
     print("Inside function:",var)

var=5
print("Outide Function:",var)
fun()
print("Outide Function:",var)

11. Enable a function to change the value of variable existing outside or Enabling function to make a variable accessible outside function. This can be done using ‘global’ keyword. Here initially var=5. But when function is called, we make var a global variable hence when var value inside function is changed, then var outside function var is also changed

def fun():
     global var
     var=6
     print("Inside function:",var)

var=5    
print("Outide Function:",var)
fun()
print("Outide Function:",var)

12. Playing with list in functions – try below two examples

Example 1:
def list_check(list2):
    print("Print 1:",list1)
    print("Print 2:",list2)
    list2=[4,5,6]
    print("Print 3:",list1)
    print("Print 4:",list2)

list1=[1,2,3]
list_check(list1)
print("Print 5:",list1)
Example 2:
 def list_check(list2):
    print("Print 1:",list1)
    print("Print 2:",list2)
    list2[1]=100
    print("Print 3:",list1)
    print("Print 4:",list2)

list1=[1,2,3]
list_check(list1)
print("Print 5:",list1)

Remember:

  • When a normal variable is copied to another, its contents are copied actually
  • When a list is copied to another list, the memory location is actually copied
  • Imagine a list is assigned to another list, list2=list1. Now when list1 is assigned to another list say llist1=list3, are changed, list2 still has original content of list1
  • Imagine a list is assigned to another list, list2=list1. Now when list1 contents are changed like list1[1]=10, then list2 element is also changed.
  • Try more examples on lists and their assignments

Previous Session: https://mytechdevice.com/parameterized-functions-in-python/

Official Python Documentation: https://www.python.org/

Leave a Reply

Your email address will not be published.