en:glossary:r:recursion

A recursion s the process of repeating items in a self-similar way. In computer programming, a recursive function is a function that calls itself.

There are several classic recursive functions, like a function to calculate the factoral^{1)} of a given number :

def factorial(n): if n == 1: return 1 else: return n * factorial(n-1)

To calculate the factorial of -per example- the integer 5 (written as 5!) you feed the integer 5 a an parameter to the factorial function above:

print factorial(5)

Recursions can also apper in graphics, like on this ad for Droste cacao (see Droste_effect): A wouman holding a packet of cacao with the image of a wouman holding a packet of cacao with the image of a wouman holding a packet of cacao….

You can use python's turtle module to generate beautiful patterns like trees out of very few lines of code:

import turtle as t def y(length=100): """ paints a branch of a tree with 2 smaller branches, like an Y""" if length < 10: return # escape the function t.fd(length) # paint the thik branch of the tree t.left(30) # rotate left for smaller "fork" branch y(length * 0.6) # create a smaller branch with 2/3 the lenght of the parent branch t.right(60) # rotoate right for smaller "fork" branch y(length * 0.6) # create second smaller branch t.left(30) # rotate back to original heading t.fd(-length) # move back to original position return # leave the function, continue with calling program # calling the function the first time y(200) # create a tree, the first branch has a length of 200 pixel

an integer multiplied by all integers lower than himself but bigger than zero. Lile 5! = 5 x 4 x 3 x 2 x 1 = 120

/var/www/horst/thepythongamebook.com/data/pages/en/glossary/r/recursion.txt · Last modified: 2014/01/09 11:07 (external edit)

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International