Opstap

Deze pagina bevat uitvoerbare code.

Syntax van recursie

Leerdoel: Begrijpen wat er gebeurt als een functie zichzelf aanroept

Opdracht 1

def main():
    """
    Main functie. Roept de andere functies aan om hun werk te doen.
    """
    x = function(10)


def function(x):
    print(x)
    if x == 0:
        return
    function(x - 1)


main()

a. Wat doet de functie function?
b. Wat is de output van dit programma?
c. Gebruik Python Tutor of dit notebook om je antwoord van a en b te controleren.

# controleer jouw antwoord

Opdracht 2

def main():
    """
    Main functie. Roept de andere functies aan om hun werk te doen.
    """
    x = function(10, 2)


def function(x, y):
    print(x)
    if x == 0:
        return
    function(x - y, y)


main()

a. Wat doet de functie function?
b. Wat is de output van dit programma?
c. Wat is de output als function(10, 2) wordt vervangen met function(5, 0) d. Gebruik Python Tutor of dit notebook om je antwoord van a, b en c te controleren.

# controleer jouw antwoord

Opdracht 3

def main():
    """
    Main functie. Roept de andere functies aan om hun werk te doen.
    """
    x = function(10, 6)


def function(x, y):
    print(x)
    if x >= 20:
        return
    function(x + y, y)


main()

a. Wat doet de functie function?
b. Wat is de output van dit programma?
c. Gebruik Python Tutor of dit notebook om je antwoord van a en b te controleren.

# controleer jouw antwoord