Return Value from Function


Return Value from Function

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

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

Syntax

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

def someFunction():
    #some code
    return x

Examples

1. Return sum from add function

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

Python Program

# Define function that returns the sum of two arguments
def add(x, y):
    result = x + y
    return result

# Take two values
a = 3.14
b = 1.23

# Call the add function
output = add(a, b)
print(f'Output  : {output}')

Output

Output  : 12.0

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.