Python Return Value from Function

Return Value from Function

We can return a value from a function in Python using return keyword.

The syntax to return a single value, say x, from a function is

def someFunction():
    #some code
    return x

In this tutorial, we will learn how to return a value from a function in Python, with the help of examples.

Example

In the following program, we write add() function that can take two arguments, and return their sum.

Python Program

#define function that returns a value
def add(x, y):
    result = x + y
    return result

#read a and b from user
a = float(input('Enter a : '))
b = float(input('Enter b : '))
#compute a + b
output = add(a, b)
print(f'Output  : {output}')

Output

Enter a : 5
Enter b : 7
Output  : 12.0
Run

About function add()

  • The function can take two arguments.
  • The sum of the given two arguments is stored in result.
  • The function returns the value in result using return statement.

Summary

In this tutorial of Python Examples, we learned how to return a value from a function, with the help of well detailed example programs.