Comments above a subroutine

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

Comments above a subroutine

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line is a correct comment in Cambridge IGCSE pseudocode?
A // Calculates the area of a circle
B # Calculates the area of a circle
2. Where is the best place to put a comment that explains what a procedure does?
A After ENDPROCEDURE at the end
B On the line before PROCEDURE
3. What does the computer do with a // comment when the program runs?
A Ignores it
B Displays it with OUTPUT
4. A comment above FUNCTION Square(Num : INTEGER) RETURNS INTEGER should mention...
A What it does and what Num is
B Only the name of the programmer
5. Which comment is at the correct indentation for code that is not indented?
A // Show a greeting PROCEDURE Greet()
B // Show a greeting PROCEDURE Greet()

Match the Pairs

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

Left columnRight column
1// Calculates the area of a rectangle Atotal ← total + 1 // add one to the count
2// width and height are both INTEGER BA comment that states the parameters of the subroutine below it
3A comment is written on the line before the procedure and explains what it does CThe comment has no // at the start, so the computer treats it as code and reports an error. It should be // Calculates the area of a rectangle.
4A short end-of-line comment can explain one line of code, and the computer ignores it D// Displays a greeting for the name given PROCEDURE Greet(name : STRING)
5Calculates the area of a rectangle FUNCTION Area(w : INTEGER, h : INTEGER) RETURNS INTEGER EThe comment is placed after the procedure header instead of on the line before it. It should be written above the PROCEDURE line.
6PROCEDURE ShowScore(score : INTEGER) // Displays the score FA comment placed on the line before a function that states its purpose

Definitions

//

Fill in the Blanks

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

1.
__ Shows a welcome message. No parameters. PROCEDURE ShowMessage() OUTPUT "Welcome"
2.
// Returns N multiplied by 2 FUNCTION Double(_ : INTEGER) RETURNS INTEGER RETURN N * 2
3.
Total ← Price * Qty __ cost of all items
4.
// Shows Name on the screen. Parameter: Name _________ ShowName(Name : STRING) OUTPUT Name
5.
// Purpose: add tax to Price FUNCTION AddTax(Price : REAL) RETURNS REAL ______ Price * 1.2

Spot the Error

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

Question 1
A // Shows N doubled
B PROCEDURE Show(N : INTEGER)
C D ← N * 2
D OUTPUT D / doubled value
E ENDPROCEDURE
Question 2
A OUTPUT "Start"
B Returns a random number
C FUNCTION Roll() RETURNS REAL
D RETURN RANDOM()
E ENDFUNCTION
Question 3
A DECLARE Score : INTEGER
B Score ← 0
C // Takes 1 from the score
D PROCEDURE AddOne()
E Score ← Score + 1
F ENDPROCEDURE
Question 4
A Total ← 0
B -- Shows the total
C PROCEDURE ShowTotal()
D OUTPUT Total
E ENDPROCEDURE
Question 5
A // Shows the total price
B // A: price of one item
C // B: number of items
D PROCEDURE Cost(A : INTEGER)
E OUTPUT A * B
F ENDPROCEDURE

Answer Key

Comments above a subroutine
Section A: Quick Questions
Q1
A — // Calculates the area of a circle
Q2
B — On the line before PROCEDURE
Q3
A — Ignores it
Q4
A — What it does and what Num is
Q5
B — // Show a greeting PROCEDURE Greet()
Section B: Match the Pairs
Q1
// Calculates the area of a rectangle → A comment placed on the line before a function that states its purpose
Q2
// width and height are both INTEGER → A comment that states the parameters of the subroutine below it
Q3
A comment is written on the line before the procedure and explains what it does → // Displays a greeting for the name given PROCEDURE Greet(name : STRING)
Q4
A short end-of-line comment can explain one line of code, and the computer ignores it → total ← total + 1 // add one to the count
Q5
Calculates the area of a rectangle FUNCTION Area(w : INTEGER, h : INTEGER) RETURNS INTEGER → The comment has no // at the start, so the computer treats it as code and reports an error. It should be // Calculates the area of a rectangle.
Q6
PROCEDURE ShowScore(score : INTEGER) // Displays the score → The comment is placed after the procedure header instead of on the line before it. It should be written above the PROCEDURE line.
Section C: Definitions
Q1
// — Starts a comment. Put one on the line before a procedure or function to say what it does and what its parameters are. The computer ignores comments.
Section D: Fill in the Blanks
Q1
// Shows a welcome message. No parameters. PROCEDURE ShowMessage() OUTPUT "Welcome"
Q2
// Returns N multiplied by 2 FUNCTION Double(N : INTEGER) RETURNS INTEGER RETURN N * 2
Q3
Total ← Price * Qty // cost of all items
Q4
// Shows Name on the screen. Parameter: Name PROCEDURE ShowName(Name : STRING) OUTPUT Name
Q5
// Purpose: add tax to Price FUNCTION AddTax(Price : REAL) RETURNS REAL RETURN Price * 1.2
Section E: Spot the Error
Q1
D — A short end-of-line comment must start with //. A single / is not a comment, so the computer tries to run 'doubled value' as code. It should be: OUTPUT D // doubled value.
Q2
B — The line before the function has no // at the start, so the computer treats the words as code and gives an error. It should be: // Returns a random number.
Q3
C — The comment above a subroutine must state what it really does. AddOne adds 1 to Score, it does not take 1 away. It should be: // Adds 1 to the score.
Q4
B — Comments in IGCSE pseudocode start with //, not --. It should be: // Shows the total.
Q5
D — The comments say the procedure has two parameters, A and B, but the header only has A. The header must match the comments and the body: PROCEDURE Cost(A, B : INTEGER).