python / beginner
Snippet
Returning Values from a Function
Functions use the 'return' keyword to send a calculated result back to the caller, allowing the result to be stored in a variable.
snippet.py
1
2
3
4
5
def multiply(a, b):return a * bresult = multiply(5, 4)print(result)
Breakdown
1
return a * b
Calculates the product and sends it out of the function.
2
result = multiply(5, 4)
Calls the function and assigns the returned value (20) to 'result'.