Functions on Functions on Functions

It is important to understand that each of the functions we write can be used and called from other functions we write. This is one of the most important ways that computer programmers take a large problem and break it down into a group of smaller problems. This process of breaking a problem into smaller subproblems is called functional decomposition.

Even though this is a pretty simple idea, in practice this example illustrates many very important Python concepts, including local and global variables along with argument passing. Note that the body of square is not executed until it is called from inside the sum_of_squares function for the first time on line 6.

Also notice that when square is called (at step 8, for example), there are two groups of local variables, one for square and one for sum_of_squares. Each group of local variables is called a stack frame.

Programming Exercise

Write two functions, one called addit and one called mult.  The addit function takes one number as an input and adds 5. The mult function takes one number as an input, and multiplies that input by whatever is returned by addit, and then returns the result.