Python Function Return

In this article you will learn about python function return which is used to end a function and go back to the placed where the function was called.

python function return

What is python function return?

The return statement in python function is simply used to return value. It is used to end the execution of function call and returns the result to the caller.

Note: return is not used in outside of the function.

Return statement is the keyword in python. It is immediately return control to the next enclosing function.

Syntax: Python function return

def function_name():
   statement(s)
   .
   .
   return(expression)

Example: Python function return

def absolute_value(num):
   if num>-0:
      return num
   else:
      return -num
print(absolute_value(4))
print((absolute_value(-2))

Output:

>> 4
>> 2

In the above code return statement can contain an expression that get executed & the value is returned. If there is no expression of return statement in python code, then the function will return none object.

Example:

def greet(name):
   print("hello, "+name+". Good morning!")

Output:

>> print(greet(programiz))         #function call
>> hello programiz. Good morning!
>> None

Here the return value is the none since greet() function directly print the name and no return statement is used.

Leave a Reply