Return values
A function can accept input values, as well as returning a value to the code that calls this function. Here is an example:
def square(x):
return x**2
Call square
on integer 3 to get the square of 3
print( square(3) ) # 9
print( square(4) ) # 16
Use square(x)
to write another function:
def sum_of_square(a,b):
sum = square(a) + square(b)
return sum
Now it's easy to calculate (3*3 + 4*4)
print( sum_of_square(3, 4) ) # result: 25
Note about return statement
- Any value provided to the
return
statement is passed back to your calling code. - If a function does not have a
return
statement, then it returns the valueNone
.