We now create our own functions. Using the terminology from our supplemental textbook, we refer to a function that produces ( returns ) a value as a "fruitful function"; we refer to a function that performs a task but does not return a value (e.,g., draws a graph) as a "procedure." Note that generally, the term function refers to both procedures and fruitful functions. In addition to defining functions, we will draw diagrams to visualize how they execute.
import
the module. (
Alternative Link
, might load faster
)
return
statement. We emphasize the difference between
printing
a value and
returning
a value. (
Alternative Link
, might load faster
)
The modules shown in the above videos are here . You can download the file to follow along, but it's ok not to. We will have additional examples during lecture.
Optionally, Read: 3.4 - 3.11
Slides: individual slides for viewing , 6-up layout for printing
Examples:
simple_math.py
, height.py
, height2.py
, height3.py
, height4.py
, height5.py
, bad_swap.py
To download the above files, right-click (Windows) or command-click (Macs) on the filename and then select "Save link as". This way you choose where the files will be saved, instead of having your computer save to the default folder (from which you will later have to move your files for organization).
Questions and Answers: check the answers — after you try them on your own!