Book
Module 4 - Section 1 - While and Functions
Module 4 - Section 1 - While and Functions
Completion requirements
View
4.1.6 - Return from a Function
Return vs Print
Certain functions, such as int or str, return a value that can be used later. When a function returns a value the value is not printed in the output.
To return a value for your defined functions, you can use the return statement. The return statement cannot be used outside of a function definition.
Write the following code to Thonny script editor and save it as maxOfTwo:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def max(x, y): | |
if x >=y: | |
return x | |
else: | |
return y |
The function takes two arguments and returns the maximum of two without printing anything.
Execute the code and in CLI write the following commands and observe the Variables area :
>>> print(max(3,9))
>>> z = max(11,2)
>>> print(z)