Functions calling other functions

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Functions calling other functions

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which code shows a function calling another function?
A def main: greet
B def main(): greet()
2. A program has a helper function called get_name(). Which is the best job for that function?
A Only ask the user for their name
B Ask for a name, do the maths, draw the score and print the report
3. True or false: one function can call another function inside its body.
A True
B False
4. In a decomposition, a problem is split into boxes: 'Get score', 'Add bonus', 'Show result'. How do these boxes match Python code?
A Each box becomes its own function
B All boxes go into one long function
5. Which main() runs the helper functions in a sensible order?
A def main(): get_score show_result
B def main(): get_score() show_result()

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1def main(): greet() Adef main(): get_name() show_menu()
2def area(w, h): return w * h Bgreet is missing its brackets, so the helper function is never called. It should be greet().
3A decomposition box called 'Show the score' becomes its own function that main() can call CThe main function calls the greet function, so main uses greet as a helper to do one job
4A big problem is split up so main() calls one small helper function for each sub-problem Dshow_score() is called but was never defined with def, so Python gives a NameError. Define the helper first.
5def main(): greet main() EA small helper function that does just one job: work out an area and give it back
6def main(): show_score() main() Fdef show_score(score): print(score)

Definitions

def main(): greet()

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
def cheer(): print("Go!") def ____(): cheer()
2.
___ main(): greet() goodbye()
3.
def show(n): _____(n * n) def main(): show(5)
4.
def add(a, b): return a _ b def main(): print(add(2, 3)) main()
5.
def area(w, h): return w * h def main(): print(____(3, 4))

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A def square(n):
B return n * n
C def sum_squares(a, b)
D return square(a) + square(b)
E print(sum_squares(2, 3))
Question 2
A def get_name():
B return "Sam"
C def welcome():
D name = get_name
E print("Hi " + name)
F welcome()
Question 3
A define add_tax(price):
B return price * 1.1
C def total(price):
D return add_tax(price)
E print(total(10))
Question 4
A def show_line():
B print("-" * 10
C def show_title():
D show_line()
E print("Quiz")
Question 5
A def say_hi:
B print("Hi!")
C def main():
D say_hi()
E main()

Answer Key

Functions calling other functions
Section A: Quick Questions
Q1
B — def main(): greet()
Q2
A — Only ask the user for their name
Q3
A — True
Q4
A — Each box becomes its own function
Q5
B — def main(): get_score() show_result()
Section B: Match the Pairs
Q1
def main(): greet() → The main function calls the greet function, so main uses greet as a helper to do one job
Q2
def area(w, h): return w * h → A small helper function that does just one job: work out an area and give it back
Q3
A decomposition box called 'Show the score' becomes its own function that main() can call → def show_score(score): print(score)
Q4
A big problem is split up so main() calls one small helper function for each sub-problem → def main(): get_name() show_menu()
Q5
def main(): greet main() → greet is missing its brackets, so the helper function is never called. It should be greet().
Q6
def main(): show_score() main() → show_score() is called but was never defined with def, so Python gives a NameError. Define the helper first.
Section C: Definitions
Q1
def main(): greet() — A function can call another function inside its body. Big problems get split into small helper functions that each do one job, like boxes in a decomposition.
Section D: Fill in the Blanks
Q1
def cheer(): print("Go!") def main(): cheer()
Q2
def main(): greet() goodbye()
Q3
def show(n): print(n * n) def main(): show(5)
Q4
def add(a, b): return a + b def main(): print(add(2, 3)) main()
Q5
def area(w, h): return w * h def main(): print(area(3, 4))
Section E: Spot the Error
Q1
C — A function definition must end with a colon. It should be def sum_squares(a, b):
Q2
D — To call a helper function you need brackets. Without them, name just stores the function itself. It should be name = get_name().
Q3
A — Python uses the keyword def, not define, to create a function. It should be def add_tax(price):
Q4
B — The print( bracket is never closed. It should be print("-" * 10).
Q5
A — A function definition needs brackets after its name, even with no parameters. It should be def say_hi():