What does the 'return' statement do in a function?

0112

The return statement in a function is used to send a value back to the caller of the function. When a function executes a return statement, it immediately exits the function and provides the specified value as the result. If no value is specified, the function will return None by default.

Here's a simple example in Python:

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

result = add(3, 5)  # result is 8
print(result)

In this example, the add function returns the sum of a and b, which is then stored in the variable result.

0 Comments

no data
Be the first to share your comment!