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. A function can call another function from inside its own body.
A True
B False
2. Which function follows the 'one job per function' idea?
A def do_everything(): ask, add up, print, save and quit
B def get_total(prices): return sum(prices)
3. In a program built from small functions, what is the job of main()?
A To call the other functions in the right order
B To do all of the work itself so other functions are not needed
4. Which is the correct way to have main() run a function called show_menu()?
A def main(): show_menu
B def main(): show_menu()
5. A hierarchy chart has a top box 'Play game' with two boxes below it: 'Get guess' and 'Check guess'. How could you turn these boxes into code?
A Make one function for each box. The top function calls the two below it.
B Put all three boxes into one big function with no other functions.

Match the Pairs

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

Left columnRight column
1def main(): greet() show_score() AThe function names are missing brackets (), so greet and show_score are never called
2total = add_tax(price) BOne function is doing several jobs (asking, printing and scoring), so it is not one job per function
3Each box in a hierarchy chart can become its own function Cdef main(): get_name() play_game() main()
4A main() function calls the other functions to run the whole program Ddef draw_board(): print('Board')
5def main(): greet show_score EA function call that uses the result of another function (add_tax) to do one part of the job
6def play(): ask_name() print(score()) def ask_name(): name = input() print(name) add_points(name) Fmain() calls two smaller functions, greet() and show_score(), so each does one job

Definitions

def main():

Fill in the Blanks

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

1.
___ main(): greet() show_score()
2.
def ____(): print("Start") main()
3.
def ______(n): return n * n def area(s): return square(s) print(area(4))
4.
def main(): _____ = get_score() show_score(score)
5.
# Chart box: Calculate total def _______________(): return 5 + 10

Spot the Error

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

Question 1
A def get_name():
B return input('Name: '
C def main():
D name = get_name()
E print('Hi', name)
Question 2
A def double(n):
B n * 2
C def show(n):
D print(double(n))
E show(4)
Question 3
A def sq(n):
B return n * n
C def sum_sq(a, b)
D return sq(a) + sq(b)
Question 4
A def main():
B print('Start')
C greet
D def greet():
E print('Hello!')
F main()
Question 5
A def get_score():
B return 80
C def show():
D print(getscore())
E show()

Answer Key

Functions calling other functions
Section A: Quick Questions
Q1
A — True
Q2
B — def get_total(prices): return sum(prices)
Q3
A — To call the other functions in the right order
Q4
B — def main(): show_menu()
Q5
A — Make one function for each box. The top function calls the two below it.
Section B: Match the Pairs
Q1
def main(): greet() show_score() → main() calls two smaller functions, greet() and show_score(), so each does one job
Q2
total = add_tax(price) → A function call that uses the result of another function (add_tax) to do one part of the job
Q3
Each box in a hierarchy chart can become its own function → def draw_board(): print('Board')
Q4
A main() function calls the other functions to run the whole program → def main(): get_name() play_game() main()
Q5
def main(): greet show_score → The function names are missing brackets (), so greet and show_score are never called
Q6
def play(): ask_name() print(score()) def ask_name(): name = input() print(name) add_points(name) → One function is doing several jobs (asking, printing and scoring), so it is not one job per function
Section C: Definitions
Q1
def main(): — A function that calls other small functions, each doing one job, so the program is built in parts. Each box in a hierarchy chart can become one function.
Section D: Fill in the Blanks
Q1
def main(): greet() show_score()
Q2
def main(): print("Start") main()
Q3
def square(n): return n * n def area(s): return square(s) print(area(4))
Q4
def main(): score = get_score() show_score(score)
Q5
# Chart box: Calculate total def calculate_total(): return 5 + 10
Section E: Spot the Error
Q1
B — The closing bracket of input() is missing. The line should be return input('Name: ') so the function gives back what the user typed.
Q2
B — double() works out n * 2 but never returns it, so it gives back None. The line should be return n * 2 so show() can use the result.
Q3
C — A function definition must end with a colon. It should be def sum_sq(a, b): so the body can call sq() twice.
Q4
C — Without brackets, greet is just the name of the function and it is not called. It should be greet() so main() runs the greet function.
Q5
D — The function is called get_score, not getscore. Function names must match exactly when one function calls another.