Introduction to Functions

Question Click to View Answer

What does the following code print?

def add(num1, num2):
    return num1 + num2

print(add(3, 4))

7 is printed.

The add function takes two number arguments and returns the sum of the two numbers.

num1 and num2 are referred to as parameters.

add(3, 4) is an example of function invocation. 3 and 4 are arguments that are passed to the function when it is invoked.

When the function is called (aka invoked), the parameters are referred to as "arguments". So num1 and num2 are parameters and 3 / 4 are arguments.

What does the following code print?

def sayHi(firstName):
    return f"hi {firstName}!"

print(sayHi("bob"))

"hi bob!" is printed.

The sayHi method takes a single argument. In this example, sayHi is invoked with the "bob" string argument and returns the "hi bob!" string.

What does the following code print?

def funny():
    return "please laugh"

print(funny())

"please laugh" is printed.

The funny() function doesn't take any arguments. The function is invoked without any arguments in the parenthesis (funny()).

What does the following code print?

def loud(sound):
    return f"{sound} is noisy"

print(loud("factory", "explosion"))

This code raises a TypeError with the following message: loud() takes 1 positional argument but 2 were given. The loud function was written to only accept one argument, but two arguments were supplied when the function was invoked in this example: loud("factory", "explosion")). The code will error out when extra arguments are supplied.

What does the following code print?

def greeting():
    "hello human"

print(greeting())

None is printed because the function doesn't return anything.

The return keyword must be used for a function to return something. Every Python function should use the return keyword.

What does the following code print?

def multiply(num1, num2):
    return num1 * num2

print(multiply(10))

This code raises a TypeError with the following message: multiply() missing 1 required positional argument: 'num2'.

The multiply function was defined to take two arguments, but it's only invoked with one argument in this example, so an exception is raised.

What does the following code print?

def silly():
    return "I am quirky"

print(silly)

<function silly at 0x10a633e18> is printed.

print(silly()) would have returned "I am quirky". The function is not invoked since parenthesis were not used. This demonstrates how Python functions are first class objects.

What does the following code print?

def yummy():
    return "cookies", "candy"

a, b = yummy()
print(f"I like {a} and {b}")

"I like cookies and candy" is printed.

The yummy function returns two values. We assign the variable a to the first value returned by yummy and the variable b to the second value returned by yummy.

What does the following code print?

def heat():
  return "I am hot"

hunk = heat

print(hunk())

"I am hot" is printed.

The heat() function returns the "I am hot" string.

The hunk variable is assigned to the heat function object, so both hunk() and heat() will return the same string.