Question 1 Report
A program uses procedures and functions to organise code.
(a) Describe the difference between a procedure and a function. [2]
(b) Explain what is meant by a parameter in the context of a procedure or function. [2]
(c) Explain two benefits of using procedures and functions in a program. [4]
(d) Explain the difference between passing parameters by value and by reference. Give an example to illustrate each. [4]
(e) Explain the difference between local and global variables. State one advantage of using local variables. [3]
(f) State what is meant by a library routine. Give one example. [1]
(a) A procedure is a named block of code that performs a specific task when called. It does not return a value to the calling code; it simply carries out its instructions and control passes back. [1] A function is also a named block of code that performs a task, but it returns a value to the part of the program that called it. The returned value can be used in expressions, assignments or conditions. [1]
(b) A parameter is a variable listed in the definition (header) of a procedure or function. It acts as a placeholder for data that will be supplied when the procedure or function is called. [1] When the call is made, the actual values (called arguments) are passed in and assigned to the corresponding parameters. This allows the same code to work with different data each time it is called, without needing to be rewritten. [1]
(c) Two benefits of using procedures and functions:
(d) Passing by value: A copy of the data is passed to the procedure or function. Any changes made to the parameter inside the module do not affect the original variable in the calling code. [1] For example, if x = 10 is passed by value and the procedure sets the parameter to 20, x remains 10 in the calling code. [1]
Passing by reference: The memory address of the original variable is passed. The procedure works directly on the original data, so any changes are reflected in the calling code. [1] For example, if x = 10 is passed by reference and the procedure sets the parameter to 20, x becomes 20 in the calling code. [1]
(e) A local variable is declared inside a procedure or function and can only be accessed within that specific module. It is created when the module starts and destroyed when it finishes. [1] A global variable is declared outside all procedures and functions, at the top level of the program. It can be accessed and modified from anywhere in the code. [1] Advantage of local variables: They prevent unintended side effects because changes to a local variable cannot accidentally affect other parts of the program. This makes code more predictable and easier to debug. [1]
(f) A library routine is a pre-written, pre-tested procedure or function provided as part of a programming language's standard library (or an external library). Programmers can call it directly without writing the code from scratch. [1] Example: a random number generator function such as RANDOM(1, 100).
Everything you need to excel in your exams