Computer-Science-A-level-Ocr
-
3-3-networks8 主题
-
3-2-databases7 主题
-
3-1-compression-encryption-and-hashing4 主题
-
2-5-object-oriented-languages7 主题
-
2-4-types-of-programming-language4 主题
-
2-3-software-development5 主题
-
2-2-applications-generation6 主题
-
2-1-systems-software8 主题
-
1-3-input-output-and-storage2 主题
-
1-2-types-of-processor3 主题
-
1-1-structure-and-function-of-the-processor1 主题
-
structuring-your-responses3 主题
-
the-exam-papers2 主题
-
8-2-algorithms-for-the-main-data-structures4 主题
-
8-1-algorithms10 主题
-
7-2-computational-methods11 主题
-
7-1-programming-techniques14 主题
-
capturing-selecting-managing-and-exchanging-data
-
entity-relationship-diagrams
-
data-normalisation
-
relational-databases
-
hashing
-
symmetric-vs-asymmetric-encryption
-
run-length-encoding-and-dictionary-coding
-
lossy-and-lossless-compression
-
polymorphism-oop
-
encapsulation-oop
-
inheritance-oop
-
attributes-oop
-
methods-oop
-
objects-oop
-
capturing-selecting-managing-and-exchanging-data
-
6-5-thinking-concurrently2 主题
-
6-4-thinking-logically2 主题
-
6-3-thinking-procedurally3 主题
-
6-2-thinking-ahead1 主题
-
6-1-thinking-abstractly3 主题
-
5-2-moral-and-ethical-issues9 主题
-
5-1-computing-related-legislation4 主题
-
4-3-boolean-algebra5 主题
-
4-2-data-structures10 主题
-
4-1-data-types9 主题
-
3-4-web-technologies16 主题
-
environmental-effects
-
automated-decision-making
-
computers-in-the-workforce
-
layout-colour-paradigms-and-character-sets
-
piracy-and-offensive-communications
-
analysing-personal-information
-
monitoring-behaviour
-
censorship-and-the-internet
-
artificial-intelligence
-
the-regulation-of-investigatory-powers-act-2000
-
the-copyright-design-and-patents-act-1988
-
the-computer-misuse-act-1990
-
the-data-protection-act-1998
-
adder-circuits
-
flip-flop-circuits
-
simplifying-boolean-algebra
-
environmental-effects
parameter-passing
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 )endfunctionprintValue(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 )endfunctionprintValue(number)
Worked Example
A pseudocode recursive function, generate, is shown.
function generate(num1 : byval)if num1 > 10 then return 10else return num1 + (generate(num1 + 1) DIV 2)endifendfunction
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. |
Responses