Where procedures and functions are defined

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

Where procedures and functions are defined

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Where should a procedure or function be defined in a Cambridge IGCSE program?
A At the start, before the main program
B At the end, after the main program
2. Which layout is correct for a short program?
A CALL Greet(), then PROCEDURE Greet() ... ENDPROCEDURE
B PROCEDURE Greet() ... ENDPROCEDURE, then CALL Greet()
3. Where do the CALL statements and function calls go?
A Before the definitions, at the top of the code
B In the main program, after the definitions
4. Which part of a program comes first?
A The subroutine definitions
B The main program statements
5. Which statement about the main program is true?
A It comes after the definitions and uses them
B It comes before the definitions and defines them

Match the Pairs

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

Left columnRight column
1PROCEDURE ShowMenu() OUTPUT "1. Play" ENDPROCEDURE CALL ShowMenu() APROCEDURE Greet() OUTPUT "Hi" ENDPROCEDURE CALL Greet()
2FUNCTION Double(n : INTEGER) RETURNS INTEGER RETURN n * 2 ENDFUNCTION OUTPUT Double(4) BFUNCTION Square(n : INTEGER) RETURNS INTEGER RETURN n * n ENDFUNCTION result <- Square(5)
3Definitions come first in a short program, then the main program with the calls comes after them. CThe main program statement that calls the function is written before the function definition. The function should be defined first.
4The main program holds the calls, so a function call such as one in an assignment goes after the definition. DThe procedure is defined first. The CALL line comes after it, in the main program.
5CALL ShowMenu() PROCEDURE ShowMenu() OUTPUT "1. Play" ENDPROCEDURE EThe function is defined at the start. The main program after it uses the function call.
6OUTPUT Double(4) FUNCTION Double(n : INTEGER) RETURNS INTEGER RETURN n * 2 ENDFUNCTION FThe CALL is placed before the procedure is defined. The definition must come first, then the main program with the CALL.

Definitions

Define then call

Fill in the Blanks

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

1.
_________ Hello() OUTPUT "Hi" ENDPROCEDURE
2.
ENDPROCEDURE // Main program ____ Hello()
3.
FUNCTION Double(N : INTEGER) RETURNS INTEGER ______ N * 2 ENDFUNCTION
4.
ENDFUNCTION // Main program Answer _ Double(5)
5.
PROCEDURE Hello() ENDPROCEDURE CALL _____()

Spot the Error

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

Question 1
A PROCEDURE ShowMenu()
B OUTPUT "1. Play"
C ENDFUNCTION
D CALL ShowMenu()
Question 2
A FUNCTION Rate() RETURNS REAL
B RETURN 0.2
C ENDFUNCTION
D CALL Rate()
Question 3
A // Definitions
B FUNCTION Beep()
C OUTPUT "Beep"
D ENDPROCEDURE
E // Main program
F CALL Beep()
Question 4
A FUNCTION GetName() RETURNS STRING
B Name ← "Sam"
C OUTPUT Name
D ENDFUNCTION
Question 5
A // Definitions come first
B FUNCTION GetPi() RETURN REAL
C RETURN 3.14
D ENDFUNCTION
E // Main program
F OUTPUT GetPi()

Answer Key

Where procedures and functions are defined
Section A: Quick Questions
Q1
A — At the start, before the main program
Q2
B — PROCEDURE Greet() ... ENDPROCEDURE, then CALL Greet()
Q3
B — In the main program, after the definitions
Q4
A — The subroutine definitions
Q5
A — It comes after the definitions and uses them
Section B: Match the Pairs
Q1
PROCEDURE ShowMenu() OUTPUT "1. Play" ENDPROCEDURE CALL ShowMenu() → The procedure is defined first. The CALL line comes after it, in the main program.
Q2
FUNCTION Double(n : INTEGER) RETURNS INTEGER RETURN n * 2 ENDFUNCTION OUTPUT Double(4) → The function is defined at the start. The main program after it uses the function call.
Q3
Definitions come first in a short program, then the main program with the calls comes after them. → PROCEDURE Greet() OUTPUT "Hi" ENDPROCEDURE CALL Greet()
Q4
The main program holds the calls, so a function call such as one in an assignment goes after the definition. → FUNCTION Square(n : INTEGER) RETURNS INTEGER RETURN n * n ENDFUNCTION result <- Square(5)
Q5
CALL ShowMenu() PROCEDURE ShowMenu() OUTPUT "1. Play" ENDPROCEDURE → The CALL is placed before the procedure is defined. The definition must come first, then the main program with the CALL.
Q6
OUTPUT Double(4) FUNCTION Double(n : INTEGER) RETURNS INTEGER RETURN n * 2 ENDFUNCTION → The main program statement that calls the function is written before the function definition. The function should be defined first.
Section C: Definitions
Q1
Define then call — Write procedures and functions at the start of the code, before the main program. The main program comes after and holds the CALL statements and function calls that use them.
Section D: Fill in the Blanks
Q1
PROCEDURE Hello() OUTPUT "Hi" ENDPROCEDURE
Q2
ENDPROCEDURE // Main program CALL Hello()
Q3
FUNCTION Double(N : INTEGER) RETURNS INTEGER RETURN N * 2 ENDFUNCTION
Q4
ENDFUNCTION // Main program Answer ← Double(5)
Q5
PROCEDURE Hello() ENDPROCEDURE CALL Hello()
Section E: Spot the Error
Q1
C — A procedure definition must finish with ENDPROCEDURE, not ENDFUNCTION. The definition is written first and then the main program calls it.
Q2
D — CALL is only used for procedures. A function is called inside an expression or an OUTPUT statement, so the main program line should be OUTPUT Rate().
Q3
B — Beep is a procedure because it does not return a value and ends with ENDPROCEDURE. The header must start with PROCEDURE, not FUNCTION.
Q4
C — A function must send its value back with RETURN. OUTPUT only displays the value, so the line should be RETURN Name.
Q5
B — The function header must use the keyword RETURNS to state the return data type. It should be FUNCTION GetPi() RETURNS REAL.