Subroutines
subroutineA section of code written outside of the main program. The umbrella term for procedures and functions. 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 sequenceIn computer programming, this is a set of instructions that follow on one from another., selectionA decision within a computer program when the program decides to move on based on the results of an event. and iterationThe repetition of a block of statements within a computer program. constructs.
In the following example, the algorithmA sequence of logical instructions for carrying out a task. In computing, algorithms are needed to design computer programs. asks the user to enter two numbers. It then adds them together and, if the total is over 10, it runs the 鈥楥ountDown鈥 subroutine. If the total is not over 10, it will run the 鈥楥ountUp鈥 subroutine.
Here is the same algorithm written using pseudocode Also written as pseudo-code. A method of writing up a set of instructions for a computer program using plain English. This is a good way of planning a program before coding.:
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 鈥淓nter first number鈥, num1
input 鈥淓nter second number鈥, num2
total = num1 + num2
if total > 10
聽聽聽聽call CountDown
else
聽聽聽聽call CountUp
end if