Check your understanding of the previous reading: writing a function header, calling a function, and reading the parts of a definition.

Question 1

Identify each numbered part of the function definition and its call:

Code annotated with numbers 1 to 11: def(1) get_room(2)(name(3): str(4))(5) ->(6) int(7):(8), then the indented body(9) with a return statement whose value is room_code_number(10), then a call get_room("Smith 411") where "Smith 411" is the argument(11)

LabelCodePart of the function
1 def
2 get_room
3 name
4 str
5 )
6 ->
7 int
8 :
9 the indented lines
10 room_code_number
11 "Smith 411"

Question 2

Type the symbol that goes at the end of a function definition's header (the last character of the line that starts with def).

Show worked solution

Every header ends with a colon, e.g. def add(left: int, right: int) -> int: — the answer is :.

Question 3

What does the pass statement do?

Select one answer, then check.

  • Correct. pass only fills in a body that would otherwise be empty; it has no effect when it runs.
  • Not quite. Passing a value to a function is what an argument in a function call does; pass is a statement inside a body.
  • Not quite. That's what a return statement does.

Question 4

Mark all of the following that ARE advantages of functions.

Select every answer that applies, then check.

  • Correct. Code reuse is one of the three advantages from the reading.
  • Not an advantage. Calling a function has a small overhead of its own; functions are not a speed optimization.
  • Correct. You can test and debug a function on its own, separately from the rest of the program.
  • Not an advantage of functions specifically. Storing values in variables works the same whether or not functions exist.

Question 5

Given the function definition below, what is the name of the function?

def get_area(width: int, height: int) -> int:
    return width * height

Show worked solution

The name comes right after def, before the opening parenthesis: get_area.

Question 6

What type of thing is returned from the function below?

def is_on_fire() -> bool:
    return True

Select one answer, then check.

  • Correct. The return type in the header is bool, and the value returned, True, is a Boolean.
  • Not quite. A string would be written in quotes, and the header says bool.
  • Not quite. is_on_fire is the function; the value it returns is a Boolean.
  • Not quite. The function explicitly returns True, not None.
  • Close, but the question asks for the type of thing returned, not the value. True is a value of type Boolean.
  • Not quite. The function returns True, and the question asks for the type anyway, which is Boolean.

Question 7

Every function must return an integer or string.

Select one answer, then check.

  • Not quite. A function can also return a float, a Boolean, or None — the reading's own examples return float and bool.
  • Correct. Functions can return any type — int, str, float, bool, or None — or nothing at all (which returns None).

Question 8

Every function defined with the def keyword MUST have a name.

Select one answer, then check.

  • Correct. The header is def <name>(...)... — Python's syntax requires a name right after def.
  • Not quite. Unlike a variable, a def function always needs a name; there is no anonymous form of def.

Question 9

The keyword "def" is short for "define".

Select one answer, then check.

  • Correct. def introduces a function definition.
  • Not quite. def really is short for "define".

Question 10

Read the following code, and then fill in the blanks below.

1| def f(x:int) -> int:
2|     x = x + 1
3|     return x
4| val = 0
5| y = f(val)
6| print(y)

You can run the code on Python Tutor to step through and see what happens.

The line executed first is line . The line executed second is line . After line 3 is executed, the next line executed is line .

Word bank — not all numbers are used

Question 11

You have been tasked with creating a function that opens a garage door. Which of the following are good, valid names?

Pick every name below that is both syntactically valid Python and a good choice.

Select every answer that applies, then check.

  • Valid Python, but not a good name: ogd doesn't tell another programmer what the function does.
  • Correct. Valid characters only, starts with a verb, and it's clear what the function does.
  • Not valid. Function names can only contain letters, numbers, and underscores — spaces aren't allowed.
  • Valid Python, but not a good name: it's a noun, not a verb, so it doesn't say what action the function performs.
  • Valid Python, but not concise — a good name captures the idea briefly, not with a full quoted line.