Exercises on functions

For background, please read the functions introduction.

This function is not properly defined, and will give a SyntaxError. Fix it, then run the call below to confirm it is working.

function subtract(p, q):
    r = p - q
    return r
  File "<ipython-input-1-87c325e2fe31>", line 1
    function subtract(p, q):
             ^
SyntaxError: invalid syntax
subtract(5, 10)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-2-72cca5aa7a33> in <module>
----> 1 subtract(5, 10)

NameError: name 'subtract' is not defined

This function also gives a SyntaxError. Fix and run.

def add(first_value, second_value)
    return first_value + second_value
  File "<ipython-input-3-4e2ecb2d0786>", line 1
    def add(first_value, second_value)
                                      ^
SyntaxError: invalid syntax
add(2, 3)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-099fca3a3426> in <module>
----> 1 add(2, 3)

NameError: name 'add' is not defined

Here is another error. Fix and run.

def cube(a_variable):
return a_variable ** 2
  File "<ipython-input-5-705b80aec482>", line 2
    return a_variable ** 2
    ^
IndentationError: expected an indented block
cube(3)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-6-e4baa36c98f8> in <module>
----> 1 cube(3)

NameError: name 'cube' is not defined

Why does the second cell below give you an error?

def add_then_multiply(a, b):
    added = a + b
    return added * b
result = add_then_multiply(10, 4)
result + added
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-8-e94650c27589> in <module>
      1 result = add_then_multiply(10, 4)
----> 2 result + added

NameError: name 'added' is not defined

Write a function called “increase” that

  • accepts two arguments

  • the body multiplies the first argument by 2 and the second by 3, adds the two resulting numbers, and returns the result.

If your function is right, the cell below should return 13.

# Your function here
increase(2, 3)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-10-7042a94be204> in <module>
----> 1 increase(2, 3)

NameError: name 'increase' is not defined

This function will run, but it probably doesn’t give you the result you expect. Fix to give the result you expect and run.

def divide(p, q):
    # Give result of dividing p by q
    result = p / q
divide(10, 2)

Remember that, in function world, the function can see the variables at the top level, but it cannot change what value the top-level variables point to.

Read the code the below, but do not run it.

Predict what you will see. Will it be an error? If not, what value will you see?

Run it to see if you’re right.

a = 12

def my_function(b):
    return a * b

my_function(4)
48

This is now going into deeper water.

We know that, in function world, the function can see the variables at the top level, but it cannot change what value the top-level variables point to.

Read the code the below, but do not run it.

Predict what you will see. Will it be an error? If not, what value will you see?

Run it to see if you’re right. See if you can explain why you are right. Call your instructor over to check your explanation.

a = 12

def function_two(b):
    a = b
    return a * b

function_two(4)
16