Programming constructs - EduqasSubroutines

Programs are created using common building blocks, known as programming constructs. These programming constructs form the basis for all programs and are also used in algorithms.

Part ofComputer ScienceSystems analysis

Subroutines

are small blocks of code in a modular program designed to perform a particular task. Since a subroutine is in itself a small program, it can contain any of the , and constructs.

In the following example, the asks the user to enter two numbers. It then adds them together and, if the total is over 10, it runs the ‘CountDown’ subroutine. If the total is not over 10, it will run the ‘CountUp’ subroutine.

Flowchart for an algorithm designed to get a total of ten by adding two numbers together

Here is the same algorithm written using :

declare CountDown while total <> 0 output total total = total – 1 repeat end Subroutine declare CountUp i is integer set i = 0 while i <> total output i i = i + 1 repeat end Subroutine num1 is integer num2 is integer total is integer input “Enter first number”, num1 input “Enter second number”, num2 total = num1 + num2 if total > 10 call CountDown else call CountUp end if