From Wikibooks, open books for an open world
Note: Some people find this section useful, and some find it confusing. If you find it confusing, feel free to skip it and continue with the next section.
To show more advanced ways of using functions, we'll now 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 result = mult(3, 2) print("3 * 2 = ", result)
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 multiplication: e.g. 3 + 3 (addition) gives the same result as 3 * 2 (multiplication).
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
result = mult(3, 2)
is run.
mult(3, 2)
to the variable result
.
mult(3, 2)
return?
mult
function to find out.
a
gets the value 3 assigned to it and the variable b
gets the value 2 assigned to it.
if b == 0:
is run. Since b
has the value 2 this is false so the line return 0
is skipped.
rest = mult(a, b - 1)
is run. This line sets the local variable rest
to the value of mult(a, b - 1)
. The value of a
is 3 and the value of b
is 2 so the function call is mult(3,1)
mult(3, 1)
?
mult
with the parameters 3 and 1.
a
has the value 3 and b
has the value 1. Since these are local values these do not affect the previous values of a
and b
.
b
has the value 1 the if statement is false, so the next line becomes rest = mult(a, b - 1)
.
mult(3, 0)
to rest.
a
has the value 3 and b
has the value 0.
if b == 0:
. b
has the value 0 so the next line to run is return 0
return 0
do?
mult(3, 0)
has the value 0. Now we know what the line rest = mult(a, b - 1)
did since we have run the function mult
with the parameters 3 and 0. We have finished running mult(3, 0)
and are now back to running mult(3, 1)
. The variable rest
gets assigned the value 0.
value = a + rest
is run next. In this run of the function, a = 3
and rest = 0
so now value = 3
.
return value
is run. This returns 3 from the function. This also exits from the run of the function mult(3, 1)
. After return
is called, we go back to running mult(3, 2)
.
mult(3, 2)
?
a = 3
and b = 2
and were examining the line rest = mult(a, b - 1)
.
rest
get 3 assigned to it. The next line value = a + rest
sets value
to 3 + 3
or 6.
result = mult(3, 2)
which can now assign the value 6 to the variable result
.
print("3 * 2 = ", result)
is run.
3 * 2 =
and the value of result
which is 6. The complete line printed is 3 * 2 = 6
.
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 is 3 * 2
is first converted into 3 + 3 * 1
. Then 3 * 1
is converted into 3 + 3 * 0
. Then we know that any number times 0 is 0 so 3 * 0
is 0. Then we can calculate that 3 + 3 * 0
is 3 + 0
which is 3
. Now we know what 3 * 1
is so we can calculate that 3 + 3 * 1
is 3 + 3
which is 6
.
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
Programming constructs solving a problem by solving a smaller version of the same problem are called recursive. In the examples in this chapter, recursion is realized by defining a function calling itself. This facilitates implementing solutions to programming tasks as it may be sufficient to consider the next step of a problem instead of the whole problem at once. It is also useful as it allows to express some mathematical concepts with straightforward, easy to read code.
Any problem that can be solved with recursion could be re-implemented with loops. Using the latter usually results in better performance. However equivalent implementations using loops are usually harder to get done correctly.
Probably the most intuitive definition of recursion is:
Try walking through the factorial example if the multiplication example did not make sense.
factorial.py
#defines a function that calculates the factorial def factorial(n): if n == 0: return 1 if n<0: return "Error, negative numbers do not have factorial values!!" return n * factorial(n - 1) print("2! =", factorial(2)) print("3! =", factorial(3)) print("4! =", factorial(4)) print("5! =", factorial(5)) print("-3! =", factorial(-3))
Output:
2! = 2 3! = 6 4! = 24 5! = 120 -3! = Error, negative values do not have factorial values!!
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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4