Recursion

A little bit about recursion:

Most computer programming languages support recursion by allowing a function to call itself within the program text. Function calling itself directly or indirectly is called as recursive function.

((no need to explain indirect recuision?? below??)) When a function calls itself, it's direct recursion. Indirect recursion occurs when a function is called not by itself but by another function that it called (either directly or indirectly). For example, if f calls f, that is direct recursion, but if f calls g which calls f, then that is indirect recursion of f.

Attention!!

Terminating condition must be known in advance to avoid infinite loop.

Let's use countDown(n) as example to show the simplest application of recursion:

Example, countDown(n)

def countDown(n):
    print(n)
    if n > 0:
        countDown(n-1)

# show even numbers within n, ? use or not??
def evenNums(n):
    print(n)
    if n > 0:
        evenNums(n-2)

evenNums(6)       # 6, 4, 2, 0

Explanation:

countDown(6) first call print(6) to display result 6, then it checks the limiting condition n>0 to be True and continues to call the function itself with new input argument 6-1, countDown(5) is executed to print out another result 5.

After this the condition is checked again, and repeat the previous procedures until the condition is False when n = 0, so the program stops here. We get displayed message as:

6
5
4
3
2
1
0

===((where to put below??)) We can use the timer function in CodeCraft to design a recursive situation.

Example, slowPrint(i)

def slowPrint(i):
  print(i)

  if i < 10:
    game.set_timer(0.2, slowPrint, i+2)

slowPrint(1)

Run and check the console result: 1,3,5,7,9,11 are going to appear one after another every 0.2 seconds.

Note:

When function 'slowPrint()` is called back in timer function as an input argument, there is no parenthesis right after it to enclose it's parameters.

===

((below examples don;t belong here??

Optional Exercises:

# block_xy --> block_xyN--> blocks_xyN2, then vary z

def blocks_xy(x,y,z,m,d):
    block(x,y,z,m)
    if x<60:
        game.set_timer(0.5,blocks_xy, x+d,y+d,z,m,d)

def blocks_xyN(x,y,z,m,d,n):
    block(x,y,z,m)
    if x< (n-1)*d:
        game.set_timer(0.5,blocks_xyN, x+d,y+d,z,m,d,n)
blocks_xyN(0,1,-20,'obsidian',3,5)        

def blocks_xyN2(x,y,z,m,d,n):
    block(x,y,z,m)
    if y< (n-1)*d:
        game.set_timer(0.5,blocks_xyN2, x-d,y+d,z,m,d,n)
blocks_xyN2(0,1,-20,'obsidian',3,5)  

for k in range(5):
    blocks_xyN(0,1,-40+2*k,'diamond',3,5)
    blocks_xyN2(0,1,-40+2*k,'diamond',3,5)

light up street lights

# street lights at x=0, height 10, 7 bulbs

def pole(y=1,z):
    block(0,y,z,'obsidian')
    if y<10:
        game.set_timer(0.2,pole,y+1,z)
# pole(0,1,-20)

def bulb(z):
    blocks_xyN(0,11,z,'diamond',2,4)
    blocks_xyN2(0,11,z,'diamond',2,4)

def bulbOn(z):
    blocks_xyN(0,11,z,'wool_yellow',2,4)
    blocks_xyN2(0,11,z,'wool_yellow',2,4)

def light(z):
    pole(1,z)
    game.set_timer(1,bulb,z)

#light(-20)  

def lightUp(z):
    pole(1,z)
    game.set_timer(1,bulb,z)
    game.set_timer(3,bulbOn,z)

#lightUp(-30)    

def lights(z,n):
    for k in range(z,(z+n*10), 10):
        light(k)

#lights(-80, 10)    

def bulbsOn(z,n):
    for k in range(z,(z+n*10), 10):
        bulbOn(k)
#bulbsOn(-80,5)

def lightsUp(z,n):

    lights(z,n)
    game.set_timer(4,bulbsOn,z,n)

lightsUp(-40,5)

results matching ""

    No results matching ""