Computer ScienceExam code: 0984

Procedures and Functions

Programming

Both procedures and functions can take .

A parameter is a variable named in the definition of the sub-program; it holds the value passed in by the caller.

An argument is the actual value supplied at the call site.

If CalculateArea is defined with the parameters Length and Width and called as CalculateArea(5, 3):

  • Length and Width are the parameters.
  • 5 and 3 are the .

Parameters can be of any data type

PROCEDURE Greet(Name : STRING, Age : INTEGER)
   OUTPUT "Hello, ", Name, ", you are ", Age, " years old"
ENDPROCEDURE

CALL Greet("Alice", 17)

Multiple parameters in order

The order of arguments must match the order of parameters:

FUNCTION Subtract(X : INTEGER, Y : INTEGER) RETURNS INTEGER
   RETURN X - Y
ENDFUNCTION

OUTPUT Subtract(10, 3)       // 10 - 3 = 7
OUTPUT Subtract(3, 10)       // 3 - 10 = -7 (different result!)

Why parameters matter

Parameters make sub-programs reusable with different inputs. Without parameters, a procedure that calculated the area of a rectangle would only ever work for one specific length and width. With parameters, the same procedure works for any length and any width the caller chooses.

Common exam question

Why programmers use procedures, functions and parameters

Question: Explain why a programmer would use procedures and parameters, or how procedures and functions make a program easier to maintain (3–4 marks).

Asked in 2 of the 17 papers, one mark per point. For procedures and functions: they split the program into smaller, manageable parts, making it easier to read, understand and debug (a second mark); they can be re-used in this program or others and run from one line, so repeated code is cut and the program gets shorter; a well-named procedure documents what that part does; and separate procedures let a team share the work.

For parameters: they pass values from the main program into the procedure or function, the values are used inside it, and so the same sub-program can be re-used with different data. Asked together, each half is capped at three of the four marks, so include at least one parameter point. Keep to the sub-programs: comments and indentation answer a different question.

Quick check

4 short questions on this note. See what has stuck, and where to look if something hasn't.

About 2 minutes

Build on this topic