The return Statement in Python

PythonServer Side ProgrammingProgramming

The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.

Example

All the above examples are not returning any value. You can return a value from a function as follows −

 Live Demo

#!/usr/bin/python
Function definition is here
def sum( arg1, arg2 ):
   # Add both the parameters and return them."
   total = arg1 + arg2
   print "Inside the function : ", total
   return total;
# Now you can call sum function
total = sum( 10, 20 );
print "Outside the function : ", total

Output

When the above code is executed, it produces the following result −

Inside the function : 30
Outside the function : 30
raja
Published on 30-Jan-2020 06:22:56
Advertisements