Python Function with return value

Sometimes we might need the result of the function to be used in further processes. Hence, a function should also return a value when it finishes its execution. This can be achieved by a return statement. 
A return statement is used to end the execution of the function call and “returns” the result (value of the expression following the return keyword) to the caller. The statements after the return statements are not executed. If the return statement is without any expression, then the special value None is returned.

Syntax: 

def fun():
statements
.
.
return [expression]

Example: 

Python program to demonstrate return statement.

Python3




def add(a, b):
 
    # returning sum of a and b
    return a + b
 
def is_true(a):
 
    # returning boolean of a
    return bool(a)
 
# calling function
res = add(2, 3)
print("Result of add function is {}".format(res))
 
res = is_true(2 < 5)
print("\nResult of is_true function is {}".format(res))


Output: 

Result of add function is 5

Result of is_true function is True


Python User defined functions

A function is a set of statements that take inputs, do some specific computation, and produce output. The idea is to put some commonly or repeatedly done tasks together and make a function so that instead of writing the same code again and again for different inputs, we can call the function. Functions that readily come with Python are called built-in functions. Python provides built-in functions like print(), etc. but we can also create your own functions. These functions are known as user defines functions.

Table of Content 

  • User defined functions 
  • Parameterized functions 
    • Default arguments 
    • Keyword arguments  
    • Variable length arguments  
    • Pass by Reference or pass by value? 
       
  • Function with return value  

Similar Reads

Python User-defined functions

All the functions that are written by any of us come under the category of user-defined functions. Below are the steps for writing user-defined functions in Python....

Python Parameterized Function

...

Python Function with return value

The function may take arguments(s) also called parameters as input within the opening and closing parentheses, just after the function name followed by a colon....