Non-Programmer's Tutorial for Python 2.6/Advanced Functions Example

Some people find this section useful, and some find it confusing. If you find it confusing you can skip it (or just look at the examples.) Now we will do a walk through for the following program:

Basically this program creates a positive integer multiplication function (that is far slower than the built in multiplication function) and then demonstrates this function with a use of the function. This program demonstrates the use of recursion, that is a form of iteration (repetition) in which there is a function that repeatedly calls itself until an exit condition is satisfied. It uses repeated additions to give the same result as mutiplication: e.g. 3 + 3 (addition) gives the same result as 3 * 2 (multiplication).

RUN 1
 * Question: What is the first thing the program does?
 * Answer: The first thing done is the function mult is defined with all the lines except the last one.


 * This creates a function that takes two parameters and returns a value when it is done. Later this function can be run.


 * What happens next?
 * The next line after the function,  is run.


 * What does this line do?
 * This line will assign the return value of  to the variable.


 * And what does  return?
 * We need to do a walkthrough of the  function to find out.

RUN 2
 * What happens next?
 * The variable  gets the value 3 assigned to it and the variable   gets the value 2 assigned to it.


 * And then?
 * The line  is run.  Since   has the value 2 this is false so the line   is skipped.


 * And what then?
 * The line  is run.  This line sets the local variable   to the value of  .  The value of   is 3 and the value of   is 2 so the function call is


 * So what is the value of  ?
 * We will need to run the function  with the parameters 3 and 1.

RUN 3
 * So what happens next?
 * The local variables in the  new run of the function are set so that  has the value 3 and   has the value 1. Since these are local values these do not affect the previous values of   and.


 * And then?
 * Since  has the value 1 the if statement is false, so the next line becomes.


 * What does this line do?
 * This line will assign the value of  to rest.


 * So what is that value?
 * We will have to run the function one more time to find that out. This time  has the value 3 and   has the value 0.


 * So what happens next?
 * The first line in the function to run is .   has the value 0 so the next line to run is


 * And what does the line  do?
 * This line returns the value 0 out of the function.


 * So?
 * So now we know that  has the value 0.  Now we know what the line   did since we have run the function   with the parameters 3 and 0.  We have finished running   and are now back to running  . The variable   gets assigned the value 0.


 * What line is run next?
 * The line  is run next. In this run of the function,   and   so now.


 * What happens next?
 * The line  is run.  This returns 3 from the function. This also exits from the run of the function  .  After   is called, we go back to running.


 * Where were we in ?
 * We had the variables  and   and were examining the line.


 * So what happens now?
 * The variable  get 3 assigned to it.  The next line   sets   to   or 6.


 * So now what happens?
 * The next line runs, this returns 6 from the function. We are now back to running the line  . Now the return value can be assigned to the variable.


 * What happens next?
 * The next line after the function,  is run.


 * And what does this do?
 * It prints  and the value of   which is 6. The complete line printed is


 * What is happening overall?
 * Basically we used two facts to calculate the multiple of the two numbers. The first is that any number times 0 is 0 .  The second is that a number times another number is equal to the first number plus the first number times one less than the second number .  So what happens is   is first converted into  .  Then   is converted into  .  Then we know that any number times 0 is 0 so   is 0.  Then we can calculate that   is   which is  .  Now we know what   is so we can calculate that   is   which is.

This is how the whole thing works:

mult(3, 2) 3 + mult(3, 1) 3 + 3 + mult(3, 0) 3 + 3 + 0 3 + 3 6

Should you still have problems with this example, look at the process backwards. What is the last step that happens? We can easily make out that the result of  is . Since   is , the function will return  and stop.

So what does the previous step do? does not return because  is not. So the next lines are executed: , which is , which is  as we just worked out. So now the variable  is set to.

The next line adds the value of  to , and since   is   and is, the result is.

Now we know that the function  returns. But we want to know the result of. Therefore, we need to jump back to the start of the program and execute it one more round: sets  to the result of. We know from the last round that this result is. Then  calculates as  , i. e. . Then the result of 3 * 2 is printed as 6.

The point of this example is that the function  starts itself inside itself. It does this until  reaches   and then calculates the result as explained above.

Recursion
Programming constructs of this kind are called recursive and probably the most intuitive definition of recursion is:


 * Recursion
 * If you still don't get it, see recursion.

These last two sections were recently written. If you have any comments, found any errors or think I need more/clearer explanations please email. I have been known in the past to make simple things incomprehensible. If the rest of the tutorial has made sense, but this section didn't, it is probably my fault and I would like to know. Thanks.

Examples
factorial.py

Output: 2! = 2 3! = 6 4! = 24 5! = 120

countdown.py

Output: 5 4 3 2 1 0

Commented_mult.py

Commented_factorial.py

Commented_countdown.py