Structured programs are built from small named subroutines. A procedure is called as a statement of its own; a function returns a single value that replaces the call in an expression. Learn to define and call both, use the exact terminology examiners expect, and write efficient pseudocode.
A procedure is a named block of code that performs a task; it may have none, one or more parameters and is run with a CALL statement. A function also performs a task but returns a single value: its header ends RETURNS data type, it uses RETURN, and it is used inside an expression, where the return value replaces the call. The values passed in a call are arguments; the identifiers in the header that receive them are parameters. Subroutines give modular, reusable code that is easier to test and maintain.
Use a procedure when a task is needed in more than one place, or to break a large problem into separate modules, and the task does not need to hand back a single value (it outputs something, or changes data). Step through the lab's procedure and function programs to watch control jump to the subroutine and back.
PROCEDURE ShowLine(Length : INTEGER, Symbol : CHAR)
DECLARE Line : STRING
DECLARE Count : INTEGER
Line ← ""
FOR Count ← 1 TO Length
Line ← Line & Symbol
NEXT Count
OUTPUT Line
ENDPROCEDURE
PROCEDURE ShowBanner()
CALL ShowLine(20, '=')
OUTPUT "Weekly quiz results"
CALL ShowLine(20, '=')
ENDPROCEDURE
CALL ShowBanner()
CALL ShowLine(5, '*')
PROCEDURE ShowBanner() is called with CALL ShowBanner().CALL ShowBanner() runs ShowBanner, which first calls ShowLine with Length 20 and Symbol '='.CALL ShowLine(5, '*') outputs *****.CALL ShowLine(12, '-'): the arguments 12 and '-' are matched in order to the parameters Length and Symbol.A procedure does not return a value, so X ← ShowLine(5, '*') is wrong. If the main program needs a result back, write a function instead.
Use a function when the subroutine must work out one value for the code that calls it. A function call is not a complete statement: it is part of an expression, and when RETURN runs, the returned value replaces the call and the expression is then evaluated. CALL is never used with a function.
FUNCTION CountChar(Text : STRING, Target : CHAR) RETURNS INTEGER
DECLARE Count : INTEGER
DECLARE Index : INTEGER
Count ← 0
FOR Index ← 1 TO LENGTH(Text)
IF MID(Text, Index, 1) = Target THEN
Count ← Count + 1
ENDIF
NEXT Index
RETURN Count
ENDFUNCTION
Total ← CountChar("banana", 'a') * 2
IF CountChar(Word, 'e') > 2 THEN
OUTPUT "Lots of e"
ENDIF
FUNCTION, the name, the parameters with their types, then RETURNS and the type of the value returned.Total ← CountChar("banana", 'a') * 2 and give the final value of Total.| Index | MID(Text, Index, 1) | Count |
|---|---|---|
| 0 | ||
| 1 | b | 0 |
| 2 | a | 1 |
| 3 | n | 1 |
| 4 | a | 2 |
| 5 | n | 2 |
| 6 | a | 3 |
CountChar("Mississippi", 's') return?CALL CountChar("banana", 'a') loses the mark: a function is never CALLed, and its return value would be thrown away.
Four quick checks. Each correct answer earns XP and records this skill on your profile.
Which statement correctly runs the procedure ShowLine?
What is the main difference between a function and a procedure?
In Floor ← Area(4.0, 2.5), what are 4.0 and 2.5?
In the lab, PROCEDURE Stars(Number : INTEGER) is run by CALL Stars(3). What is Number?
Questions often show a subroutine and ask you to identify its parts, so learn these words precisely. Then use subroutines and sensible loop choices to write efficient pseudocode.
| Term | Meaning | In FUNCTION Area(Length : REAL, Width : REAL) RETURNS REAL |
|---|---|---|
| Header | the first line: the subroutine's name, its parameters and (for a function) the return type | the whole line |
| Interface | how data passes in and out: the parameters with their data types and order, and the return type | two REALs in, one REAL out |
| Parameter | an identifier in the header that receives a value when the subroutine is called | Length, Width |
| Argument | the actual value or variable passed in the call | 4.0 and 2.5 in Floor ← Area(4.0, 2.5) |
| Return value | the single value a function sends back to replace the call | 10.0 |
// efficient: stops as soon as the item is found
FUNCTION FindItem(SearchItem : INTEGER) RETURNS INTEGER
// Data is a global ARRAY[1:1000] OF INTEGER
DECLARE Index : INTEGER
Index ← 1
WHILE Index <= 1000
IF Data[Index] = SearchItem THEN
RETURN Index
ENDIF
Index ← Index + 1
ENDWHILE
RETURN -1
ENDFUNCTION
Floor ← Area(4.0, 2.5), identify the arguments and give the return value, assuming Area returns Length * Width.Do not mix up parameter and argument. The parameter is the name in the header (Length); the argument is what is passed in the call (4.0).
Unlocks once the four checks above are done. Worth more XP, written to AS Paper 1 and Paper 2 standard.
CountChar is a function returning an INTEGER. Which statement is NOT a valid use of it?
When is a function more appropriate than a procedure?
What is meant by the interface of a subroutine?
Square(N) returns N * N. Total is 10. What is Total after Total ← Total + Square(4)?
This skill is now recorded on your profile. Keep the chain going.