November 2017
Beginner to intermediate
204 pages
5h 23m
English
In addition to passing in information when a function is executed, it is also possible to retain information afterward using the return statement. A return statement is simply the word, return, followed by the value to be returned, and is written at the end of the code block:
def myFunction(): <some code> return <return value>
The return value is captured outside of the function by assigning the function call to a variable. In the following example, I've modified the code slightly to print the reaction to the weather outside of the function:
def reactToTheWeather(isSunny): if isSunny: return "Nice Weather!" else: return "No sun today!"reaction1 = reactToTheWeather(True)print(reaction1)reaction2 = reactToTheWeather(True) ...
Read now
Unlock full access