Non-Programmer's Tutorial for Python/Advanced Functions Example
From Wikibooks, the open-content textbooks collection
Now we will do a walk through for the following program:
def mult(a, b): if b == 0: return 0 rest = mult(a, b - 1) value = a + rest return value print "3 * 2 = ", mult(3, 2)
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 iteration (repetition) within a function; the function 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).
- Question: What is the first thing the program does?
- Answer: The first thing done is the function mult is defined with the lines:
def mult(a, b): if b == 0: return 0 rest = mult(a, b - 1) value = a + rest return value
- 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,
print "3 * 2 = ", mult(3, 2)is run. - And what does this do?
- It prints
3 * 2 =and the return value ofmult(3, 2) - And what does
mult(3, 2)return? - We need to do a walkthrough of the
multfunction to find out. - What happens next?
- The variable
agets the value 3 assigned to it and the variablebgets the value 2 assigned to it. - And then?
- The line
if b == 0:is run. Sincebhas the value 2 this is false so the linereturn 0is skipped. - And what then?
- The line
rest = mult(a, b - 1)is run. This line sets the local variablerestto the value ofmult(a, b - 1). The value ofais 3 and the value ofbis 2 so the function call ismult(3,1) - So what is the value of
mult(3, 1)? - We will need to run the function
multwith the parameters 3 and 1. - So what happens next?
- The local variables in the new run of the function are set so that
ahas the value 3 andbhas the value 1. Since these are local values these do not affect the previous values ofaandb. - And then?
- Since
bhas the value 1 the if statement is false, so the next line becomesrest = mult(a, b - 1). - What does this line do?
- This line will assign the value of
mult(3, 0)to rest. - So what is that value?
- We will have to run the function one more time to find that out. This time
ahas the value 3 andbhas the value 0. - So what happens next?
- The first line in the function to run is
if b == 0:.bhas the value 0 so the next line to run isreturn 0 - And what does the line
return 0do? - This line returns the value 0 out of the function.
- So?
- So now we know that
mult(3, 0)has the value 0. Now we know what the linerest = mult(a, b - 1)did since we have run the functionmultwith the parameters 3 and 0. We have finished runningmult(3, 0)and are now back to runningmult(3, 1). The variablerestgets assigned the value 0. - What line is run next?
- The line
value = a + restis run next. In this run of the function,a = 3andrest = 0so nowvalue = 3. - What happens next?
- The line
return valueis run. This returns 3 from the function. This also exits from the run of the functionmult(3, 1). Afterreturnis called, we go back to runningmult(3, 2). - Where were we in
mult(3, 2)? - We had the variables
a = 3andb = 2and were examining the linerest = mult(a, b - 1). - So what happens now?
- The variable
restget 3 assigned to it. The next linevalue = a + restsetsvalueto3 + 3or 6. - So now what happens?
- The next line runs, this returns 6 from the function. We are now back to running the line
print "3 * 2 = ", mult(3, 2)which can now print out the 6. - 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 (
x * 0 = 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 (x * y = x + x * (y - 1)). So what happens is3 * 2is first converted into3 + 3 * 1. Then3 * 1is converted into3 + 3 * 0. Then we know that any number times 0 is 0 so3 * 0is 0. Then we can calculate that3 + 3 * 0is3 + 0which is3. Now we know what3 * 1is so we can calculate that3 + 3 * 1is3 + 3which is6.
This is how the whole thing works:
3 * 2 3 + 3 * 1 3 + 3 + 3 * 0 3 + 3 + 0 3 + 3 6
[edit] 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.
[edit] Example
factorial.py
#defines a function that calculates the factorial def factorial(n): if n <= 1: return 1 return n * factorial(n - 1) print "2! =", factorial(2) print "3! =", factorial(3) print "4! =", factorial(4) print "5! =", factorial(5)
Output:
2! = 2 3! = 6 4! = 24 5! = 120
countdown.py
def count_down(n): print n if n > 0: return count_down(n-1) count_down(5)
Output:
5 4 3 2 1 0

