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.

Lessons - watch before lecture

Note: Alternative links to watch the pre-lecture videos were added below on 2/17. If the original links performed poorly (e.g., slow loading) for your locale, try the alternative links.

  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)
  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)
  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)
  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)

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.

Optional reading: 3.4 - 3.11 (html version)

Lecture materials

Slides: individual slides for viewing, 6-up layout for printing

Examples: simple_math0.py, simple_math.py, simple_math2.py, height.py, height2.py, height3.py

Lecture Recording

Answers to the 6 questions at the end of the lecture slides (slides 50, 57, 59, 61, 63, 65): Do those visualization exercises to check your understanding of this lecture! See the lecture slides file above for the questions. After drawing the diagrams yourself, you can check your answers here.