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...

Assessment: Computer Science (9-1) 0984 | Paper 1 Mock 01 | Computer Systems Subject: Computer Science (9-1) - 0984

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]

Answer Details

(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:

  1. Code reuse: Once written, a procedure or function can be called from multiple places in the program without duplicating code. [1] This reduces the program's length, minimises the chance of errors from copying code, and means a fix in one place automatically applies everywhere the module is used. [1]
  2. Modularity: Breaking a large program into smaller, self-contained modules makes it easier to understand, test and debug. [1] Each module can be developed and tested independently before being integrated, and different team members can work on different modules simultaneously. [1]

(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).

Download The App On Google Playstore

Everything you need to excel in your exams

Green Bridge CBT Mobile App
Personalized AI Learning Chat Assistant
200,000+ Exam Questions Across IGCSE, JAMB, WAEC & NECO
Over 3,900 Lesson Notes
Offline Support - Learn Anytime, Anywhere
Green Bridge Timetable
Literature Summaries & Potential Questions
Track Your Performance & Progress
In-depth Explanations for Comprehensive Learning