Parameter Passing (OCR A Level Computer Science)

Revision Note

Parameter Passing

What are Parameters?

  • A parameter is a piece of data that is input into a code block such as a function or a procedure so that it can complete its task

  • For example this could be a password that the user has entered which is passed into a code block as a parameter so it can check it meets password complexity rules

  • When passing parameters into a code block, two standard methods are used:

    • Passing by value (also known as byVal) 

    • Passing by reference (also known as byRef) 

  • The choice of which method to use depends on the specific requirements of the code block and how the programmer wants to handle data modifications inside and outside of this.

Parameter Passing by Value

  • When a parameter is passed by value, a copy of the actual value is made and assigned to the function's parameter

  • Any changes to the parameter within the function's scope do not affect the original value outside the function

  • The function works with and changes its own local copy of the value

  • This approach ensures that the original data remains unchanged, making it suitable when you want to protect the original data from being modified within the function

  • When the function ends, the copy of the value is destroyed

  • However, it can lead to performance overhead when working with large data structures since a copy is made

Pseudocode example

In this example pseudocode, a variable called number is assigned a random number between 1 and 10. This is then passed into the function printValue by value

number = random (1,10)
function printValue(number : byVal) // value is passed by value
  return("The random value is:", number )
endfunction
printValue(number)

Parameter Passing by Reference

  • When a parameter is passed by reference, the function receives the memory address of the value rather than a copy of the value

  • Any changes made to the parameter within the function directly affect the original value outside the function

  • Both the function and the calling code share the same memory location

  • This approach allows functions to directly modify the original data, making it efficient for working with large data structures

  • However, it can also lead to unexpected side effects if not handled carefully, as changes made within the function affect other parts of the program

Pseudocode example

In this example pseudocode, a variable called number is assigned a random number between 1 and 10. This is then passed into the function printValue by reference

number = random (1,10)
function printValue(number : byRef) // value is passed by reference
  return("The random value is:", number )
endfunction
printValue(number) 

Worked Example

A pseudocode recursive function, generate, is shown.

function generate(num1 : byval)
if num1 > 10 then
   return 10
else
   return num1 + (generate(num1 + 1) DIV 2)
endif
endfunction

The parameter, num1, is passed by value. Explain why the parameter was passed by value instead of by reference.

2 marks

How to answer this question:

  • You need to demonstrate your understanding of passing by parameter and by reference

  • You must refer to the parameter in the question, num1

Answer:

If the value is passed by value, num1 will not be overridden as it is a copy of the parameter that is used, and this will produce the correct output.

Acceptable answers you could have given instead:

If the parameter had been passed by reference, it would not produce the correct result, as num1 would be overridden because it is a pointer to the address of the variable.

Comparison

Aspect

Passing by Value

Passing by Reference

Parameter Handling

A copy of the argument's value is assigned to the function's parameter. The function works with its own local copy of the value.

The function receives a reference or memory address of the argument, allowing direct access and modification of the original data.

Data Protection

Passing by value protects the original data from being modified within the function. Any changes made to the parameter do not affect the original argument outside the function.

Passing by reference allows functions to directly modify the original data, possibly leading to unintended changes outside the function if not handled carefully.

Performance Overhead

Copying data for passing by value can incur performance overhead, especially when working with large data structures or objects.

Passing by reference avoids data copying, making it efficient, especially for large data structures.

Memory Consumption

Passing by value can increase memory consumption due to the creation of a copy of the data for the function call.

Passing by reference does not duplicate data, improving memory efficiency, especially in memory-constrained environments.

Complex Data Structures

Suitable for passing primitive types (e.g. numbers, strings) or small data structures.

Well-suited for working with large data structures (e.g. arrays, lists) without creating copies, reducing memory usage.

Data Protection

Passing by value is useful when the original data remains unaltered, providing data protection.

Passing by reference is appropriate when the function needs to manipulate and update the original data directly.

Scope Limitation

Changes made to the parameter within the function do not spread to the calling code, preserving data integrity outside the function.

Changes made to the parameter directly affect the original data outside the function, which can lead to side effects and unintended behaviour.

Function Reusability

Passing by value can enhance function reusability since the function does not modify the original data.

Passing by reference may reduce function reusability as the function may unexpectedly affect the caller's data.

Convenience

It is easy to implement and understand, especially for simple data types.

It requires careful handling to prevent unintended changes to data outside the function, making it more complex to manage.

You've read 0 of your 0 free revision notes

Get unlimited access

to absolutely everything:

  • Downloadable PDFs
  • Unlimited Revision Notes
  • Topic Questions
  • Past Papers
  • Model Answers
  • Videos (Maths and Science)

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?