Procedures versus functions

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

Procedures versus functions

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which best describes a procedure?
A It carries out a task, such as printing, without returning a value
B It always sends a value back to the code that called it
2. Which best describes a function?
A It returns a value to the code that called it
B It can never be used inside an expression or stored in a variable
3. Which of these is a procedure? def show_welcome(name): print("Welcome,", name)
A show_welcome, because it only prints and has no return
B show_welcome, because it has a parameter
4. Which of these is a function?
A def say_hi(): print("Hi")
B def double(n): return n * 2
5. Which line stores the result of a call in a variable?
A total = show_message("Hi") # show_message only prints
B total = add(3, 4) # add returns a value

Match the Pairs

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

Left columnRight column
1def show_menu(): print("1. Play") Ashow_menu()
2def add(a, b): return a + b BA function: it sends a value back to the code that called it so the result can be stored or used
3A function's returned value can be stored in a variable for later use CA procedure: it carries out a task (printing) but does not return a value to the caller
4A procedure is called as its own statement because it has no result to store Darea only prints and has no return, so it is a procedure. size stores None, not 12. Use return w * h to make it a function.
5def area(w, h): print(w * h) size = area(3, 4) Etotal = add(2, 3)
6def greet(name): return "Hi " + name greet("Sam") Fgreet returns a value but the result is never printed or stored, so nothing is shown. Use print(greet("Sam")) or store it in a variable.

Definitions

procedure vs function

Fill in the Blanks

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

1.
def showTotal(a, b): _____(a + b)
2.
def add(a, b): ______ a + b
3.
def add(a, b): return a + b total = ___(2, 3)
4.
___ showMenu(): print("1. Play") print("2. Quit")
5.
def showArea(w, h): print(w * h) ________(3, 4)

Spot the Error

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

Question 1
A def say_hi(name):
B return "Hi, " + name
C say_hi("Ava")
Question 2
A def area(w, h):
B result = w * h
C return
D print(area(3, 4))
Question 3
A def show_menu():
B print("1. Play")
C print("2. Quit")
D show_menu
Question 4
A def square(n):
B n * n
C answer = square(5)
D print(answer)
Question 5
A def get_double(n)
B return n * 2
C print(get_double(4))

Answer Key

Procedures versus functions
Section A: Quick Questions
Q1
A — It carries out a task, such as printing, without returning a value
Q2
A — It returns a value to the code that called it
Q3
A — show_welcome, because it only prints and has no return
Q4
B — def double(n): return n * 2
Q5
B — total = add(3, 4) # add returns a value
Section B: Match the Pairs
Q1
def show_menu(): print("1. Play") → A procedure: it carries out a task (printing) but does not return a value to the caller
Q2
def add(a, b): return a + b → A function: it sends a value back to the code that called it so the result can be stored or used
Q3
A function's returned value can be stored in a variable for later use → total = add(2, 3)
Q4
A procedure is called as its own statement because it has no result to store → show_menu()
Q5
def area(w, h): print(w * h) size = area(3, 4) → area only prints and has no return, so it is a procedure. size stores None, not 12. Use return w * h to make it a function.
Q6
def greet(name): return "Hi " + name greet("Sam") → greet returns a value but the result is never printed or stored, so nothing is shown. Use print(greet("Sam")) or store it in a variable.
Section C: Definitions
Q1
procedure vs function — A procedure carries out a task without returning a value, while a function returns a value to the code that called it, so the result can be stored or used.
Section D: Fill in the Blanks
Q1
def showTotal(a, b): print(a + b)
Q2
def add(a, b): return a + b
Q3
def add(a, b): return a + b total = add(2, 3)
Q4
def showMenu(): print("1. Play") print("2. Quit")
Q5
def showArea(w, h): print(w * h) showArea(3, 4)
Section E: Spot the Error
Q1
B — say_hi is a procedure that should print its message. With return, the text goes back to the caller, which does nothing with it, so nothing is shown. It should be print("Hi, " + name).
Q2
C — A function must send its answer back to the caller. A bare return gives back None, so print() would show None instead of 12. It should be return result.
Q3
D — Without brackets the procedure is only named, not called, so its code never runs and nothing prints. It should be show_menu().
Q4
B — The multiplication is worked out but never returned. The function gives back None, so answer would be None. It should be return n * n.
Q5
A — A function definition header must end with a colon. It should be def get_double(n):.