Contents

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)
5

Here’s sub in action:

sub(4, 1)
3

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)
18

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)
  File "<ipython-input-6-e81e2f8282d1>", line 1
    my_func1 = # Your code here
               ^
SyntaxError: invalid syntax

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)
  File "<ipython-input-8-f54b5b103cbd>", line 1
    another_func = # Your code here
                   ^
SyntaxError: invalid syntax