Mar-25-2023, 02:45 PM
The return statement returns values from a function.
This part:
This part:
All functions return a value, even if they don't contain a return statement. If a function doesn't have a return statement, the return value is None.
What you tried to do is called "pass by reference". In pass by reference, a function is passed arguments meant to hold the return value for the function. Python does not support pass by reference. In Python you pass arguments used by the body of the function, and use "return" to return values from the function. To use the function return values, the caller must assign the return value(s) to a variable.
def add(a, b): result = a + b return result total = add(2, 3) print(total)
Output:5
This part:def add(a, b):creates a function named "add" and says it expects 2 arguments.
This part:
result = a + b return resultis the body of the function. The body defines what the function does. Here the body adds the arguments a and b and returns the result.
This part:
total = add(2, 3)calls the function and assigns the return value to a variable named "total".
All functions return a value, even if they don't contain a return statement. If a function doesn't have a return statement, the return value is None.
What you tried to do is called "pass by reference". In pass by reference, a function is passed arguments meant to hold the return value for the function. Python does not support pass by reference. In Python you pass arguments used by the body of the function, and use "return" to return values from the function. To use the function return values, the caller must assign the return value(s) to a variable.