Programming Lecture 6, Chapter 6,
Fall 2009

http://grace.evergreen.edu/mon


08/25/17 Be sure to hit refresh on your browser to get the latest version.

Overview

This chapter on functions covers one of the four most important programming structures we have encountered so far:
  1. Loops
  2. Sequences (especially lists)
  3. Objects and their methods
  4. Functions

The goal of this chapter is to understand when and how to create a function and to understand how the function call and return mechanism works.

These notes are intended to be used as a guide to working through the chapter and trying sections of code.

Section 6.1 - The Function of Functions

Look at the program on p166. Take a pencil and put a box around the two duplicate blocks of code in this program. Each of the two blocks will cover 4 lines of code Underline the few differences in each of the two blocks.

Section 6.2 - Functions, Informally

After studying this section, make sure you can read the happy.py program at the bottom of p170 and top of p171. Type in this program and try it. Follow the flow of control with your finger, that is, follow the sequence of steps that are executed in the program from beginning to end. You will be jumping around from main to the function sing to the function happy and back again several times.

In main the sing function is called with actual parameter "Fred". As the flow of control is passed to the sing function, the formal parameter person is bound to the value "Fred" just like an assignment statement. The formal parameter person cannot be used outside the sing function - it is a local variable.

The happy function has no parameters (just like main). The main() is actually a function itself that by convention in all languages is the starting point of your program. In many languages main is called by the operating system automatically when starting your program.

This is a good time to test your knowledge by writing the program oldMac.py, problem 1 in the programming exercises. How many parameters will you need? Go through the lyrics and square every place the animal name occurs and go through the lyrics again and circle every place the animal sound occurs. This should give you a hint as to how to parameterize your song function.

Section 6.3 - Future Value with a Function

Look at the futval_graph3.py function in this section. Notice the defined function drawBar at the beginning of the program. This function replaces the two blocks of code you boxed in Section 6.1. Look at p173 and notice where drawBar is called with three actual parameters. Box these two function calls in your book. Compare the code for drawBar with the two blocks of code you boxed earlier in Section 6.1 - the code is reproduced for you on p171. Notice where the formal parameters in drawBar are used to stand for different actual parameters in the two different calls to drawBar. That lets drawBar act like each of the two boxed sections of code from Section 6.1 as needed. If you understand this section, then you are will on your way to understanding the use of functions.

Now work this through on your own. What are the actual parameters in each of the calls to drawBar on p173? What formal parameters are they bound to in the drawBar function?

Do you see how the binding of actual parameters to formal parameters will accomplish both of the blocks of the orginal code shown on p171?

Note that there are two variables year - one inside the drawBar function and one outside the drawBar function. These are two distinct variables that happen to have the same name for convenience.

Section 6.4 - Functions and Parameters: The Details

This section gives the details of the parameter passing mechanism of Python and all other programming languages. You just have to follow this section closely to see what is happening in the step-by-step process of calling a function and passing parameters to a function.

Note that the second paragraph of this section reminds you that local variables can only be accessed inside a function. We call this a scope rule for local variables.

Section 6.5 - Getting Results (back) from a Function

Functions can return values as well as take inputs. There is nothing magic about returning values - it works just like regular math functions you are used to. The sequence of code on p178 shows how this works. The main thing is to remember to put the return statement in your function code.

Read the program on p179 and see what it does.

Type in and try the program on p179.

The code in the middle of p180 shows how to return two or more values from a function. This is a little unusual, but handy.

The code at the bottom of p180 and top of p181 shows what to expect if you forget to put in the return statement in a function, a common oversight.

The last part of the section (p181 subsection 6.5.2 - functions that modify parameters) introduces a more advanced understanding of parameters that are objects. Follow the three programs add_interest.py and see how information can and can't be passed out of functions.

Section 6.6 - Functions and Program Structure

This section emphasizes the importance of functions for organizing larger programs to keep them more comprehensible. The outcome of this section is the very nicely organized program futval_graph4 on p190.

Look at how much shorter and easier it is to read the main() block of the program at the bottom of 190. You can more easily see what the program does without being distracted by the details of how to draw the initial labeled window or the bars.