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.

Lecture Prep

Watch:
  1. Procedures (6 minutes) We define our first procedure, which is a function that does not produce (return) a value. For now we define a procedure right in the Python interactive shell and then call it. ( Alternative Link , might load faster )
  2. Function Modules (4 minutes) Typically we define functions in a module. To call a module function you must first import the module. ( Alternative Link , might load faster )
  3. Fruitful Functions (7 minutes) A fruitful function includes the return statement. We emphasize the difference between printing a value and returning a value. ( Alternative Link , might load faster )
  4. Function Visualization (11 minutes) We introduce the call frame , which is where function variables live. We learn to visualize how a function executes. ( 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


Lecture Materials

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!

Link to Lecture Recording