Large programs are built from small named blocks of code. Learn how to define and call procedures and functions in 0478 pseudocode, pass values in as parameters, return a value from a function, and tell local variables from global ones.
A procedure is a named block of code that performs a task; it is run with CALL as a statement on its own and does not return a value. A function is a named block of code that returns a single value, so it is used inside an expression; its header states the data type it RETURNS. Parameters are the variables in the header that receive the values passed in when it is called (up to three in the exam). A local variable is declared inside a subroutine and can only be used there; a global variable is declared in the main program and can be used anywhere.
In the lab, choose Procedure with a parameter and step through it. The call stack shows when control has passed into the procedure and when it comes back to the main program.
PROCEDURE Stars(Number : INTEGER)
DECLARE Count : INTEGER
FOR Count ← 1 TO Number
OUTPUT "*"
NEXT Count
ENDPROCEDURE
CALL Stars(3)
OUTPUT "Done"
CALL Stars(3) passes the value 3 into it.The same idea in Python (for comparison only: the exam uses pseudocode)
def stars(number):
for count in range(number):
print("*")
stars(3)
print("Done")
CALL Stars(2) then CALL Stars(1). How many stars are output?A procedure with no parameters is still called with CALL, e.g. CALL DrawLine. Do not write a procedure call inside an expression: it has no value to give back.
A function works like a procedure, but it hands a single value back to the place where it was called. The RETURN statement sets that value.
FUNCTION SumSquare(Number1 : INTEGER, Number2 : INTEGER) RETURNS INTEGER
RETURN Number1 * Number1 + Number2 * Number2
ENDFUNCTION
OUTPUT "Sum of squares = ", SumSquare(10, 20)
SumSquare(10, 20) returns 100 + 400 = 500, so the output is Sum of squares = 500.
| Procedure | Function | |
|---|---|---|
| Returns a value? | No | Yes, exactly one |
| Header | PROCEDURE Name(...) | FUNCTION Name(...) RETURNS type |
| How it is called | CALL Name(...) as a statement | inside an expression, e.g. X ← Name(...) |
The same idea in Python (for comparison only: the exam uses pseudocode)
def sum_square(number1, number2):
return number1 * number1 + number2 * number2
print("Sum of squares =", sum_square(10, 20))
FUNCTION Larger(A : INTEGER, B : INTEGER) RETURNS INTEGER returns A if A > B, otherwise B. What is output by OUTPUT Larger(4, 9) + Larger(7, 2)?Never use CALL with a function. Its value must be used: assigned, output or part of a calculation, e.g. Total ← SumSquare(2, 3).
Four quick checks. Each correct answer earns XP and records this skill on your profile.
What is the main difference between a function and a procedure?
Which statement correctly runs the procedure Stars so it outputs 4 stars?
In the lab's Local and global variables program, what is output for the global Total?
What is a parameter?
The lab's Local and global variables program shows the same idea: the procedure has its own Total, so the global Total is not changed. Here is a similar program.
PROCEDURE AddBonus
DECLARE Total : INTEGER // local: only exists inside AddBonus
Total ← 5
OUTPUT "Inside: ", Total
ENDPROCEDURE
DECLARE Total : INTEGER // global: declared in the main program
Total ← 100
CALL AddBonus
OUTPUT "Outside: ", Total
The output is Inside: 5 then Outside: 100. The local Total is a separate variable that disappears when the procedure ends.
PROCEDURE AddTen contains only Score ← Score + 10, with no local declaration. The main program declares Score globally, sets Score ← 5, then calls AddTen twice and outputs Score. What is output?The lab program outputs Local Total 6 and then Global Total 10: assigning to a local variable with the same name never touches the global one.
Unlocks once the four checks above are done. Worth more XP, written to IGCSE Paper 1 standard.
Using the SumSquare function in this lesson, what does SumSquare(3, 4) return?
Which line is a correct use of the function SumSquare?
Which is a benefit of using local variables in a procedure?
Which is a correct function header in 0478 pseudocode?
This skill is now recorded on your profile. Keep the chain going.