Basis#

Functies schrijven#

Leerdoel: Schrijven van simpele functies

Deze opgave bestaat uit het maken van verschillende functies. Vergeet niet elke functie te testen met assertions en vergeet de docstrings niet.

Opdracht 1#

Schrijf de functie tpl(x) die een getal als argument accepteert en drie keer de waarde van dat argument teruggeeft.

In : tpl(4)
Out: 12

In: tpl("hoi")
Out: "hoihoihoi"
# jouw oplossing

Opdracht 2#

a. Schrijf de functie min_two(a, b) die twee getallen als argument accepteert en de kleinste waarde teruggeeft.

b. Schrijf de functie min_three(a, b, c) die drie getallen als argument accepteert en de kleinste waarde teruggeeft.

# jouw oplossing

Opdracht 3#

Schrijf de functie absolute(x,y) die twee getallen accepteert en de afstand berekent tussen de twee getallen.

In : absolute(3, 10)
Out: 7

In: absolute(-3, 10)
Out: 13
# jouw oplossing

Syntax van recursie#

Leerdoel: Begrijpen wat er gebeurt als een functie zichzelf aanroept

Opdracht 1#

def main():
    """
    Main functie. Roept de andere functies op 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 op 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 op 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