Revise Computingrevisecomputing.co.uk
Free student revisionSee questionsFor schoolsSchool enquiry
Free revisionSee questionsFor schoolsSchool enquiry
Knowledge/A Level/H446/02 Algorithms and programming/Programming Techniques/Modularity, functions and procedures…

Modularity, functions and procedures, parameter passing by value and by reference

Modularity, functions and procedures, parameter passing by value and by reference.

2.2.1 Programming Techniques

Programming Techniques — Note 4 of 6

Candidates need to understand what is meant by modular code and how it is produced using functions and procedures, including the differences between them. Candidates need to understand the purpose of parameters and the difference between passing by value and by reference, recommending which to use for a given situation. Candidates need to be able to read, trace and write code that uses functions, procedures and parameters.

What you need to know

Modular programs are built from subroutines, each carrying out one task. Functions return a value; procedures do not. Parameters can be passed by value (a copy) or by reference (the original), which affects whether the subroutine can change the caller's data.

What to know

  • By value: safer, as the original cannot be changed accidentally, but copying large data uses memory and time. By reference: efficient for large data structures and allows a subroutine to return more than one change, but the original can be changed unintentionally.

Key terms

Exam tipCommon mistake

Modular programming

Breaking a program into separate subroutines or modules, each performing one task, that can be written and tested independently.

Function

A subroutine that returns a value to the point where it was called.

Procedure

A subroutine that carries out a task but does not return a value.

Parameter

A variable in the subroutine definition that receives a value (argument) when the subroutine is called.

  • In OCR pseudocode, parameters are passed by value unless stated; where it matters, byVal and byRef are used.
  • Exam tip: Trace by-reference parameters carefully: any change to the parameter inside the subroutine changes the caller's variable.
Passing by value

A COPY of the argument is passed; changes inside the subroutine do not affect the original variable.

Passing by reference

The ADDRESS of the original variable is passed; changes inside the subroutine DO affect the original variable.

Code examples

Passing by value and by reference (OCR pseudocode)
text
procedure update(x:byVal, y:byRef)
    x = x + 10
    y = y + 10
endprocedure

a = 1
b = 1
update(a, b)
print(a)   // 1  (only the copy changed)
print(b)   // 11 (the original changed)
← Previous noteGlobal and local variables2.2.1 Programming TechniquesNext note →Use of an IDE to develop/debug a program2.2.1 Programming Techniques

Back to Programming Techniques

Programming Techniques

Note 4 of 6

  1. 1Programming constructs: sequence, iteration, branching
  2. 2Recursion, how it can be used and how it compares to an iterative approach
  3. 3Global and local variables
  4. 4Modularity, functions and procedures, parameter passing by value and by reference
  5. 5Use of an IDE to develop/debug a program
  6. 6Use of object oriented techniques
Topic overview