Procedures and Functions · Cambridge syllabus 8.1 · Lesson 1 of 3
Procedures
A procedure groups a task under a name so it can be called from different places. Parameters supply information needed for the task. Local variables belong to the call and help avoid accidental interference with other code.
By the end, you can
- Understand the purpose of a procedure.
- Define and call procedures.
- Use up to three parameters, each with its data type.
- Explain how procedures improve program structure.
- Use local and global variables.
Procedures
A procedure is a named block of statements that performs a task and is executed by calling its name. It runs, and then control returns to the statement after the call.
It is declared with PROCEDURE PrintHeader, the statements, and ENDPROCEDURE, and called by writing CALL PrintHeader. The block exists once however many times it is called, and procedures are defined at the start of the code, before the main program that calls them.
Parameters let one procedure serve many cases. PROCEDURE DrawLine(Size : INTEGER) declares a parameter with its data type; CALL DrawLine(20) passes a value into it, and inside the procedure Size holds 20 for that call. A procedure can have up to three parameters, separated by commas, such as PROCEDURE ShowScore(Name : STRING, Score : INTEGER). A procedure with parameters is general, which is why one DrawLine replaces twenty near-identical blocks.
The reason to use them is that repeated code is a liability. The same eight lines in five places must be corrected in five places, and one will be missed. Written once and called five times, a fix happens once. A procedure also names a section of logic, so the main program reads as a list of what happens rather than a thousand lines of how.
A procedure should do one thing, and its name should say what. If the name needs an and in it, it is probably two procedures.
Variables have a scope. A local variable is declared inside a procedure or function: it exists only while that procedure runs and can only be used inside it, so two procedures can each have a variable called Count without affecting each other. A global variable is declared in the main program and can be used anywhere, including inside procedures. Local variables are safer, because no other part of the program can change them by accident.
Worth remembering
- A named block called with CALL; control returns after the call.
- Up to three parameters, each with a data type, make one procedure serve many cases.
- Write once, call many times: a fix then happens in one place.
- Local variables exist only inside their procedure; global ones can be used anywhere.
Worked example
PROCEDURE DisplayGreeting(Name : STRING) can print a message for whichever name is passed to it by CALL DisplayGreeting("Sam").
Practice
Warm-up · not marked
Check your understanding
Answer in your own words, then compare with the worked answer. Nothing here is saved or scored.
Worked answer
Repeated logic is kept in one place, so it can be corrected or changed once.
Check your method, units and explanation against it.
Marked practice
Set 1 of 5 · easiest first · Checked as you go, with the worked answer. Sign in or create a free account to save your progress.