Function as values exercises

Download notebook Interact

For background, please read the functions as values page.

Consider these two functions:

def add(a, b):
    return a + b
def sub(a, b):
    return a - b

Here’s add in action:

add(4, 1)

Here’s sub in action:

sub(4, 1)

There’s some code below, that will error, because the assignment statement does not set func to have the value we need. Set func correctly so the result equals 2:

func = add
func(10, 8)

Set my_func1 and my_func2 in the code fragment below, so that the result is 12:

my_func1 = # Your code here
my_func2 = # Your code here
my_func1(8, 2) + my_func2(3, 3)

Here is a function that takes three arguments. The first, called f, should be set to a function value - that is, a value that is the internal representation of a function. The second and third values, called x and y, should set to be number values.

def do_it(f, x, y):
    return f(x, y)

Set another_func so the result returned is 4:

another_func = # Your code here
do_it(another_func, 1, 3)