Desk checking an algorithm that calls a function

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

Desk checking an algorithm that calls a function

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. When desk checking an algorithm that calls a function, what should you record first in the function's trace table?
A The argument values passed in, shown under the parameter columns
B The final output of the whole program
2. A function returns a value to its caller. What do you do with that value in the desk check?
A Leave it in the function's table and ignore it
B Carry it back and write it into the caller's trace table
3. Which statement about variables in a desk check is correct?
A A function's local variables share columns with the caller's variables
B A function's local variables get their own columns, separate from the caller's variables
4. The caller runs total = add(4, 6) and add(a, b) returns a + b. What goes in the function's returned value row?
A 10
B total
5. The same function is called twice with different arguments. How should you desk check it?
A Trace each call separately, using its own argument values
B Trace it once and reuse the same results for both calls

Match the Pairs

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

Left columnRight column
1result ← double(5) Atotal ← addTax(50) define addTax(price)
2define double(n) return n * 2 BThe caller's table uses the parameter name n instead of the returned value. n is local to the function and does not exist in the caller.
3The argument column in the caller must match the parameter column in the function: the argument value is copied into the parameter when the call starts. CIn the caller's trace table, record the argument 5 and then write the value returned by double() into the result column.
4A variable created inside the function is local, so it gets a column in the function's trace table only, not in the caller's table. Only the returned value goes back to the caller. Ddefine area(w, h) a ← w * h return a
5Caller table: x = 4, then result = n EIn the function's own trace table, the parameter column n receives the argument value, then the return row records n * 2.
6Function table: n = 3, a = 9, return row left blank FThe returned value row was never filled in, so the caller has nothing to carry back into its trace table.

Definitions

desk check

Fill in the Blanks

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

1.
define double(n) return n * 2 result ← ______(4)
2.
define add(a, b) return a + _ sum ← add(3, 4)
3.
define half(n) ______ n / 2 x ← half(10)
4.
x ← 4 result ← double(_) OUTPUT result
5.
define addTax(p) total ← p * 1.1 return _____

Spot the Error

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

Question 1
A def double(n):
B result = n * 2
C print(result)
D total = double(5)
Question 2
A def add(a, b):
B return a + c
C x = 4
D y = add(x, 3)
Question 3
A def area(w, h):
B return w * h
C size = area(3)
D print(size)
Question 4
A def average(a, b):
B total = a + x
C return total / 2
D x = average(6, 10)
Question 5
A def square(n, m):
B return n * n
C answer = square(4)
D print(answer)

Answer Key

Desk checking an algorithm that calls a function
Section A: Quick Questions
Q1
A — The argument values passed in, shown under the parameter columns
Q2
B — Carry it back and write it into the caller's trace table
Q3
B — A function's local variables get their own columns, separate from the caller's variables
Q4
A — 10
Q5
A — Trace each call separately, using its own argument values
Section B: Match the Pairs
Q1
result ← double(5) → In the caller's trace table, record the argument 5 and then write the value returned by double() into the result column.
Q2
define double(n) return n * 2 → In the function's own trace table, the parameter column n receives the argument value, then the return row records n * 2.
Q3
The argument column in the caller must match the parameter column in the function: the argument value is copied into the parameter when the call starts. → total ← addTax(50) define addTax(price)
Q4
A variable created inside the function is local, so it gets a column in the function's trace table only, not in the caller's table. Only the returned value goes back to the caller. → define area(w, h) a ← w * h return a
Q5
Caller table: x = 4, then result = n → The caller's table uses the parameter name n instead of the returned value. n is local to the function and does not exist in the caller.
Q6
Function table: n = 3, a = 9, return row left blank → The returned value row was never filled in, so the caller has nothing to carry back into its trace table.
Section C: Definitions
Q1
desk check — Trace by hand: record each function call's argument values, follow its statements to the return, then carry the returned value back into the caller's trace table.
Section D: Fill in the Blanks
Q1
define double(n) return n * 2 result ← double(4)
Q2
define add(a, b) return a + b sum ← add(3, 4)
Q3
define half(n) return n / 2 x ← half(10)
Q4
x ← 4 result ← double(x) OUTPUT result
Q5
define addTax(p) total ← p * 1.1 return total
Section E: Spot the Error
Q1
C — The function prints the value instead of returning it, so there is no returned value to carry back into the caller's trace table and total would be None. It should be 'return result'.
Q2
B — c is not a parameter or a local variable, so it has no column in the function's trace table. The function should use its own parameters: 'return a + b'.
Q3
C — The function has two parameters (w and h), so the call needs two argument values. Desk checking would find h has no value. It should be 'size = area(3, 4)'.
Q4
B — x is a caller variable that does not exist yet when the function runs. A function should only use its own parameters and local variables. It should be 'total = a + b'.
Q5
A — The call passes one argument (4) but the header now has two parameter columns, so m would never get a value. The header should be 'def square(n):'.