Meaningful identifiers for arrays, procedures and functions

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

This Meaningful identifiers for arrays, procedures and functions worksheet is part of the IGCSE Computer Science 0478 — 8.1a Data Types and Variables journey. Print it as a booklet?

Meaningful identifiers for arrays, procedures and functions

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which is the better identifier for an array that stores student names?
A StudentNames
B x
2. Which is the better name for a function that works out a total?
A Total_stuff
B CalculateTotal
3. Which is the better name for a procedure that shows a menu on screen?
A p1
B DisplayMenu
4. Which statement about naming arrays is correct?
A Use a noun that describes the data stored, e.g. StudentNames
B Use a verb that describes an action, e.g. FillNames
5. Which statement about naming procedures and functions is correct?
A Start with a verb to show the action, e.g. DisplayMenu
B Use a noun for the data, e.g. MenuList

Match the Pairs

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

Left columnRight column
1StudentNames APROCEDURE DisplayMenu()
2CalculateTotal BA good array identifier: a PascalCase noun that shows the array stores a list of student names
3A procedure that shows a menu on screen should have a verb-based PascalCase name CThe function name is a lowercase noun. It should be a PascalCase verb phrase such as CalculateTotal.
4An array holding exam scores should be named with a noun in PascalCase DThe array name x is meaningless. It should be a descriptive PascalCase noun such as StudentNames.
5DECLARE x : ARRAY[1:30] OF STRING EDECLARE ExamScores : ARRAY[1:30] OF INTEGER
6FUNCTION total(Price : REAL) RETURNS REAL FA good function identifier: a PascalCase verb phrase that shows the function works out a total

Definitions

PascalCase

Fill in the Blanks

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

1.
DECLARE ____________ : ARRAY[1:30] OF STRING
2.
PROCEDURE ___________() OUTPUT "1. Start" ENDPROCEDURE
3.
Total ← ______________(Price, Qty)
4.
CALL ___________() INPUT Choice
5.
FUNCTION ______________() RETURNS REAL RETURN Price * Qty ENDFUNCTION

Spot the Error

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

Question 1
A StudentNames[1] ← "Ava"
B x[2] ← "Liam"
C OUTPUT StudentNames[1]
Question 2
A PROCEDURE displaymenu()
B OUTPUT "1. Play"
C OUTPUT "2. Quit"
D ENDPROCEDURE
Question 3
A Price ← 12.5
B // Work out the total cost
C Total ← Maths(Price)
D OUTPUT Total
Question 4
A GetScores[1] ← 78
B ExamScores[2] ← 91
C OUTPUT ExamScores[2]
Question 5
A PROCEDURE DisplayMenu()
B OUTPUT "1. Play"
C ENDPROCEDURE
D CALL Menu()

Answer Key

Meaningful identifiers for arrays, procedures and functions
Section A: Quick Questions
Q1
A — StudentNames
Q2
B — CalculateTotal
Q3
B — DisplayMenu
Q4
A — Use a noun that describes the data stored, e.g. StudentNames
Q5
A — Start with a verb to show the action, e.g. DisplayMenu
Section B: Match the Pairs
Q1
StudentNames → A good array identifier: a PascalCase noun that shows the array stores a list of student names
Q2
CalculateTotal → A good function identifier: a PascalCase verb phrase that shows the function works out a total
Q3
A procedure that shows a menu on screen should have a verb-based PascalCase name → PROCEDURE DisplayMenu()
Q4
An array holding exam scores should be named with a noun in PascalCase → DECLARE ExamScores : ARRAY[1:30] OF INTEGER
Q5
DECLARE x : ARRAY[1:30] OF STRING → The array name x is meaningless. It should be a descriptive PascalCase noun such as StudentNames.
Q6
FUNCTION total(Price : REAL) RETURNS REAL → The function name is a lowercase noun. It should be a PascalCase verb phrase such as CalculateTotal.
Section C: Definitions
Q1
PascalCase — Each word starts with a capital. Name arrays with nouns (StudentNames); name functions and procedures with verbs (CalculateTotal, DisplayMenu) to show what they store or do.
Section D: Fill in the Blanks
Q1
DECLARE StudentNames : ARRAY[1:30] OF STRING
Q2
PROCEDURE DisplayMenu() OUTPUT "1. Start" ENDPROCEDURE
Q3
Total ← CalculateTotal(Price, Qty)
Q4
CALL DisplayMenu() INPUT Choice
Q5
FUNCTION CalculateTotal() RETURNS REAL RETURN Price * Qty ENDFUNCTION
Section E: Spot the Error
Q1
B — The array name x says nothing about what it stores. Arrays should have descriptive PascalCase noun names, so this line should use StudentNames[2].
Q2
A — The identifier is all lower case, so the words are hard to read. PascalCase starts each word with a capital: DisplayMenu.
Q3
C — Maths is a vague noun that does not say what the function does. Functions should have verb names such as CalculateTotal.
Q4
A — GetScores is a verb, which suggests an action. An array stores data, so it needs a noun name such as ExamScores.
Q5
D — Menu is a noun and does not match the procedure that was declared. The procedure is named with a verb, DisplayMenu, so the call should be CALL DisplayMenu().