Functions, Procedures and Module
Functions, Procedures and Module
In the same way that a textbook is divided into chapters, a program is divided into related
functionality using modules.
Programs usually integrate blocks of code and modules that have already been created in other
projects.
The algorithms a program uses are implemented as the functions and procedures in these module
In a computer program there are often sections of the program that we want to re-use or repeat.
Chunks of instructions can be given a name - they are called functions and procedures.
Algorithms can be broken down into procedures or functions. This saves time by only having
to execute (call) the function when it is required, instead of having to type out the whole
instruction set.
Programming languages have a set of pre-defined (also known as built-in) functions and
procedures. If the programmer makes their own ones, they are custom-made or user-defined.
Procedures or functions?
In a program for drawing shapes, the program could ask the user what shape to draw.
The instructions for drawing a square could be captured in a procedure. The algorithm
for this action could be a set of tasks, such as these:
Repeat the next two steps four
times: Draw a line of length n.
Turn right by 90 degrees.
If this were a computer program, this set of instructions could be given the name 'square'
and this sequence would be executed by running (calling) that procedure.
A function could calculate the VAT due on goods sold. The algorithm for this function
could be:
VAT equals (value_of_goods_sold * 0.2) Return VAT
If this were a computer program, this set of instructions could be given the name
“calculate_VAT” and would be executed by running (calling) that function.
The function would then return the value as VAT which is then used elsewhere.