Back to Challenge Set 15

Pseudocode Challenge: Procedures

Difficulty  

divider

Objective

Three problems on PROCEDURE and RETURN — Activities 2.1 to 2.3, in AP exam notation. Two translate C# into exam notation, and one traces. The trace is the one that catches people.

Work these on paper. There is no computer on exam day.


What You Are Allowed to Use

The reference sheet's procedures. Arithmetic, assignment with ←, and IF work the way they did in earlier pseudocode challenges.

A procedure
PROCEDURE procName(parameter1, parameter2)
{
<block of statements>
}
A procedure that returns
PROCEDURE procName(parameter1, parameter2)
{
<block of statements>
RETURN(expression)
}
Calling one
procName(arg1, arg2)
result ← procName(arg1, arg2)
Display
DISPLAY(expression)

Four rules that catch people:

  • RETURN takes parentheses: RETURN(width * height). C#'s return does not, which is why this one is easy to get backward.
  • There are no types. No int, no void, no string — in the header or anywhere else.
  • Assignment is ←, and there are no semicolons.
  • There is no $"..." on the reference sheet. DISPLAY shows one value and puts a space after it.

The Problems

1 — Translate

Rewrite this method and the two lines that use it in exam notation.

Problem 1 — the method
int AreaOf(int width, int height)
{
return width * height;
}
Problem 1 — the main program
int a = AreaOf(5, 3);
Console.WriteLine(a);

Then say what your version displays.


2 — Trace

What does this display?

Problem 2
PROCEDURE twice(n)
{
RETURN(n * 2)
}
x ← twice(6)
DISPLAY(x)
DISPLAY(twice(x))

Careful: the last line calls the procedure again. It does not reuse the first result — work out what its argument is first.


3 — Translate

Rewrite this in exam notation.

Problem 3
Label("Avery", 88);
void Label(string name, int score)
{
Console.WriteLine($"{name}: {score}");
}

Then answer this: one line does not translate directly. Say which one, what you wrote instead, and whether your version displays exactly the same thing.


Submit

  • Your Problem 1 translation, and what it displays
  • What Problem 2 displays, with the value of each call written beside it
  • Your Problem 3 translation, and your answer about the line that did not translate
  • One sentence: which problem took you longest, and what slowed you down?

This is graded on completion, not accuracy. Show your working for the trace — a wrong answer with visible reasoning is worth more to both of us than a right one with none.

Commence Challenge