Writing a short program with a procedure and a function

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

Writing a short program with a procedure and a function

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Where do the procedure and function definitions go in a complete program?
A At the start, before the main program
B After the main program, at the very end
2. Which line in the main program calls a procedure named ShowMenu?
A CALL ShowMenu()
B total ← ShowMenu()
3. Which main program line calls a function named AddThree that takes three parameters?
A total ← AddThree(a, b, c)
B CALL AddThree(a, b, c)
4. Which is a sensible order for the main program of a program split across subroutines?
A OUTPUT, then calculate, then INPUT
B INPUT, then calculate, then OUTPUT
5. Which main program line uses a function call directly inside OUTPUT?
A OUTPUT CALL AddThree(4, 5, 6)
B OUTPUT AddThree(4, 5, 6)

Match the Pairs

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

Left columnRight column
1CALL ShowMenu() AAddThree is a function, so it cannot be used with CALL. Its returned value must be used in an expression or OUTPUT.
2OUTPUT AddThree(2, 5, 8) BThe main program calls the procedure before it has been defined. Definitions must be listed first.
3Procedures and functions are defined at the start of the program, before the main program uses them CIn the main program, this line runs the procedure ShowMenu, then control returns to the next line
4The main program stores a function result in a variable, then uses it in a later step DIn the main program, this calls the function AddThree with three values and outputs the value it returns
5CALL ShowMenu() PROCEDURE ShowMenu() OUTPUT "1. Play" ENDPROCEDURE ETotal ← AddThree(A, B, C) OUTPUT Total
6CALL AddThree(2, 5, 8) FPROCEDURE ShowMenu() OUTPUT "1. Play" ENDPROCEDURE

Definitions

main program

Fill in the Blanks

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

1.
_________ ShowTitle() OUTPUT "Area Calculator" ENDPROCEDURE
2.
FUNCTION Square(N : INTEGER) _______ INTEGER RETURN N * N ENDFUNCTION
3.
____ ShowTitle() INPUT Side OUTPUT Square(Side)
4.
PROCEDURE ShowSum(A, B, C : INTEGER) OUTPUT A + B + _ ENDPROCEDURE
5.
INPUT Side Area ← Square(Side) ______ Area

Spot the Error

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

Question 1
A PROCEDURE ShowTitle()
B RETURN "Quiz Time"
C ENDPROCEDURE
D CALL ShowTitle()
Question 2
A FUNCTION Sq(N) RETURNS INTEGER
B OUTPUT N * N
C ENDFUNCTION
D OUTPUT Sq(5)
Question 3
A PROCEDURE Greet()
B OUTPUT "Hello"
C ENDFUNCTION
D CALL Greet()
Question 4
A FUNCTION Half(N) RETURNS REAL
B Ans <- N / 2
C RETURN Total
D ENDFUNCTION
E OUTPUT Half(9)
Question 5
A FUNCTION Show(N : INTEGER)
B OUTPUT "Score: ", N
C ENDPROCEDURE
D CALL Show(10)

Answer Key

Writing a short program with a procedure and a function
Section A: Quick Questions
Q1
A — At the start, before the main program
Q2
A — CALL ShowMenu()
Q3
A — total ← AddThree(a, b, c)
Q4
B — INPUT, then calculate, then OUTPUT
Q5
B — OUTPUT AddThree(4, 5, 6)
Section B: Match the Pairs
Q1
CALL ShowMenu() → In the main program, this line runs the procedure ShowMenu, then control returns to the next line
Q2
OUTPUT AddThree(2, 5, 8) → In the main program, this calls the function AddThree with three values and outputs the value it returns
Q3
Procedures and functions are defined at the start of the program, before the main program uses them → PROCEDURE ShowMenu() OUTPUT "1. Play" ENDPROCEDURE
Q4
The main program stores a function result in a variable, then uses it in a later step → Total ← AddThree(A, B, C) OUTPUT Total
Q5
CALL ShowMenu() PROCEDURE ShowMenu() OUTPUT "1. Play" ENDPROCEDURE → The main program calls the procedure before it has been defined. Definitions must be listed first.
Q6
CALL AddThree(2, 5, 8) → AddThree is a function, so it cannot be used with CALL. Its returned value must be used in an expression or OUTPUT.
Section C: Definitions
Q1
main program — The section after all procedure and function definitions. It calls them in order, using CALL for procedures and function calls inside expressions or OUTPUT.
Section D: Fill in the Blanks
Q1
PROCEDURE ShowTitle() OUTPUT "Area Calculator" ENDPROCEDURE
Q2
FUNCTION Square(N : INTEGER) RETURNS INTEGER RETURN N * N ENDFUNCTION
Q3
CALL ShowTitle() INPUT Side OUTPUT Square(Side)
Q4
PROCEDURE ShowSum(A, B, C : INTEGER) OUTPUT A + B + C ENDPROCEDURE
Q5
INPUT Side Area ← Square(Side) OUTPUT Area
Section E: Spot the Error
Q1
B — A procedure does not send a value back, so it cannot use RETURN. It should show the text with OUTPUT "Quiz Time".
Q2
B — A function must send its answer back to the main program with RETURN. It should be RETURN N * N, so that OUTPUT Sq(5) can show the result.
Q3
C — A procedure definition must end with ENDPROCEDURE, not ENDFUNCTION. The closing keyword has to match the opening keyword.
Q4
C — Total was never given a value in this function. The calculated value is stored in Ans, so the line should be RETURN Ans.
Q5
A — This subroutine only outputs and has no RETURN. It is a procedure, and it ends with ENDPROCEDURE, so the first line must start with PROCEDURE.