Course 8.2Lesson 1 of 3
A long program is easier to write and to test when it is cut into named parts. This lesson defines and calls procedures and functions in Cambridge pseudocode, passes values to them as parameters, and explains local and global variables.
Try the code and the simulations beside the lesson.
A recipe book says "make the sauce (page 12)" instead of writing the sauce out in every recipe. A program does the same. A named block of code that does one job is a subroutine. You write it once and call it by name as often as you need. The Python Foundations chapter "Functions: Name a Recipe" teaches this in Python with def. Cambridge pseudocode has two kinds of subroutine.
The syllabus puts procedures and functions at the start of the code, before the main program.
A procedure starts with PROCEDURE and its name, and ends with ENDPROCEDURE. It may have parameters: names, each with a data type, for the values it is given. You run it with CALL.
PROCEDURE DefaultLine
CALL Line(60)
ENDPROCEDURE
PROCEDURE Line(Size : INTEGER)
DECLARE Length : INTEGER
FOR Length ← 1 TO Size
OUTPUT '-'
NEXT Length
ENDPROCEDURECALL Line(25) passes the value 25 to the parameter Size, and 25 dashes are output. CALL DefaultLine has no parameters: it calls Line with 60. When the procedure ends, control goes back to the line after the call. A CALL is a complete statement on its own line.
The value in the call is the argument. Arguments are matched to the parameters in order, and each must have the right data type. In this syllabus a procedure or a function has at most three parameters.
A function says which type of value it returns, with RETURNS. The keyword RETURN gives the value back. It is usually the last statement.
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 , so the output is Sum of squares = 500. A function call is not a statement on its own, and it never uses CALL. It is used inside an expression: in an OUTPUT, an assignment or a condition. The returned value takes the place of the call.
In a flowchart a subroutine call has its own symbol: a rectangle with a double line at each side. The picture shows the main program calling AddBonus twice. Each time, control goes to the procedure and comes back.
Where a variable is declared decides where it can be used.
DECLARE Total : INTEGER
PROCEDURE AddBonus(Points : INTEGER)
DECLARE Bonus : INTEGER
Bonus ← DIV(Points, 10)
Total ← Total + Points + Bonus
ENDPROCEDURE
Total ← 0
CALL AddBonus(45)
CALL AddBonus(30)
OUTPUT TotalTotal is global, so the procedure can add to it. Bonus is local: it is made fresh on each call and is gone afterwards. The first call adds , the second adds , so the output is 82. A line OUTPUT Bonus in the main program would be an error, because Bonus does not exist there.
Local variables keep a subroutine separate from the rest of the program. Two subroutines can both use a local Count without changing each other's value. That makes each one easier to test and reuse.
Python is a little different. Inside a def, a variable that is given a value is local, unless you write global total first.
The program has a procedure, a function and a procedure that changes a global variable, all in Python.
CALL with a function. A function is used in an expression: Result ← SumSquare(3, 4), never CALL SumSquare(3, 4).RETURN. Every function must give back one value of the type after RETURNS.Size : INTEGER, not just Size.PROCEDURE Name(Param : TYPE) ... ENDPROCEDURE, run with CALL Name(value).FUNCTION Name(Param : TYPE) RETURNS TYPE ... RETURN value ... ENDFUNCTION, used inside an expression.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
CALL SumSquare(2, 3).CALL AddBonus(28) and CALL AddBonus(9)?Average that takes three integers and returns a real value.Bonus is local?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place in the course.