Question:

What does the return statement do in a function? Explain with the help of an example.

Show Hint

Use return to pass the result back
and continue using it elsewhere in your program.
Hide Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

In Python, the return statement is used inside a function
to send the result back to the caller.
When the return statement is executed,
the function ends immediately and the specified value is returned.
If no return is used, the function returns None by default.

Example:

def add(a, b):
    result = a + b
    return result

sum = add(5, 3)
print(sum)
In this example, the function add() returns the sum of a and b
using the return statement.
The returned value is stored in sum and printed.
Was this answer helpful?
0
0

Top Questions on Programming in Python

View More Questions