Sometimes we want our functions to provide information back to the program that called them.
A function can give a value back to the caller using the
return keyword. The code that calls the function can assign the value returned to a variable.
For example, we could change our Python program to calculate the average of two numbers, instead of printing it:
def calculate_average(num1, num2): return (num1 + num2) / 2
Now our function will return the average back to the code that called it. Let’s write some code that calls the function, assigns the returned value to a variable, and prints that variable:
avg = calculate_average(12,48) print('average is', avg)
When we called
calculate_average with the inputs of
48, the function returns a value of
30.0. We then store that returned value into the variable
avg. When we print
avg it’s equal to
30.0, resulting in the following output:
average is 30.0
We now have a
get path to function that returns a path to a particular point for
Move to go to. Use it and
move to have Codey retrieve the key, then unlock the door.
Move get path to 4 3 Move get path to 7 5