Skip to main content
ScotlandComputer ScienceSyllabus dot point

What are the building blocks of program logic, and how do sub-programs share data?

Computational constructs: assignment and arithmetic, selection with logical operators, fixed and conditional iteration, pre-defined functions, and sub-programs with parameter passing (by value and by reference) and variable scope.

An SQA Higher Computing Science answer on computational constructs, covering assignment and arithmetic, selection with logical operators, fixed and conditional loops, pre-defined functions, sub-programs, parameter passing by value and by reference, and local and global scope.

Generated by Claude Opus 4.812 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this key area is asking
  2. Assignment and arithmetic
  3. Selection and logical operators
  4. Iteration: fixed and conditional loops
  5. Pre-defined functions
  6. Sub-programs: procedures and functions
  7. Parameter passing: by value and by reference
  8. Variable scope: local and global
  9. Examples in context
  10. Try this

What this key area is asking

The SQA wants you to know the computational constructs that make up program logic: assignment and arithmetic, selection (including logical operators), fixed and conditional iteration, pre-defined functions, and sub-programs with parameter passing (by value and by reference) and variable scope.

Assignment and arithmetic

Assignment stores a value in a variable: SET total TO 0 or area = length * breadth. Arithmetic operators combine numeric values: +, -, *, /, plus integer division and modulus (MOD, the remainder). Modulus is handy for tests such as "is this number even?" (number MOD 2 = 0).

Selection and logical operators

For example: IF age >= 13 AND age <= 19 THEN SEND "teenager". A nested or multi-way selection (IF ... ELSE IF ... ELSE) handles several mutually exclusive cases, such as grading a mark into A, B, C or fail.

Iteration: fixed and conditional loops

Choosing the right loop is examined directly. Counting through a fixed-size collection is a fixed loop; waiting for an event such as correct input or a sentinel value is a conditional loop.

Pre-defined functions

Pre-defined functions are operations the language already provides, so you do not write them yourself. Common examples include rounding a real to a set number of places, finding the length of a string, generating a random number, or finding a substring. Using them saves effort and avoids errors, and they often return a value you assign to a variable: rounded = round(price, 2).

Sub-programs: procedures and functions

A sub-program is a named block of code that performs one task and can be called whenever needed. There are two kinds:

  • A procedure carries out a task but does not return a single value (for example displayMenu()).
  • A function carries out a task and returns a value (for example area(length, breadth) returns a number).

Sub-programs make a program modular: each can be written, tested and reused independently, the main program reads as a clear sequence of calls, and changes are localised.

Parameter passing: by value and by reference

So a function that only reads a price takes it by value; a procedure that sorts an array takes the array by reference so the sorted result is visible to the caller.

Variable scope: local and global

The scope of a variable is where in the program it can be used. A local variable is declared inside a sub-program and exists only there, so it cannot clash with variables elsewhere - this is preferred because it keeps sub-programs independent. A global variable is visible to the whole program; it is convenient but risky, because any part of the code can change it, making bugs hard to trace. Good design favours local variables and parameters over globals.

Examples in context

These constructs are universal. Every app uses selection (show this screen if logged in), iteration (draw each item in a list), functions (a tax calculator, a date formatter) and parameter passing. Pass-by-reference explains a classic bug: a function that sorts an array changes the caller's array because arrays are passed by reference, which surprises programmers expecting a copy. Preferring local variables is why well-structured code is easier to maintain in teams: each function is self-contained.

Try this

Q1. State which type of loop suits processing every element of an array of fixed size. [1 mark]

  • Cue. A fixed loop.

Q2. A function must change the caller's array. State the parameter-passing method required. [1 mark]

  • Cue. By reference.

Q3. Explain why local variables are preferred to global variables. [2 marks]

  • Cue. A local variable exists only inside its sub-program, so it cannot be changed by unrelated code, keeping sub-programs independent and bugs easier to trace.

Exam-style practice questions

Practice questions written in the style of SQA exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

SQA Higher (style)4 marksExplain the difference between passing a parameter by value and passing it by reference, and state when each is appropriate.
Show worked answer →

When a parameter is passed by value, a copy of the data is sent into the sub-program. Changes made to it inside the sub-program do not affect the original variable in the calling code.

When a parameter is passed by reference, the sub-program is given access to the original variable itself (its location), so any change made inside the sub-program does change the original.

Pass by value when the sub-program only needs to read the data (an input it must not alter), which is safer because the original is protected. Pass by reference when the sub-program must send a result back by changing the variable, or for large structures such as arrays where copying would be wasteful.

Markers reward by value = a copy (original unchanged), by reference = the original (changes persist), plus a correct use for each (read-only input versus returning a changed value or arrays).

SQA Higher (style)4 marksExplain the difference between a fixed loop and a conditional loop, giving a situation suited to each.
Show worked answer →

A fixed loop (for example FOR ... DO) repeats a known number of times, set before the loop starts. It suits a task where the number of repetitions is known in advance, such as processing every element of an array of 30 marks.

A conditional loop (for example WHILE ... DO or REPEAT ... UNTIL) repeats while (or until) a condition is met, so the number of repetitions is not known in advance. It suits a task such as repeatedly asking for a password until the user enters the correct one, where you cannot say beforehand how many tries it will take.

Markers reward fixed = known number of repetitions with a suitable example, and conditional = repeats based on a condition (unknown number of times) with a suitable example.

Related dot points

Sources & how we know this