Standard modules, subroutines and functions

All programming languages have standard functions. These are already-defined instructions that perform some sort of calculation on values (parameters) that are passed to it. 

For example a function may calculate the length of a text string or the square root of an integer.

 

 
We have learned that it is better to break down large and complex programming tasks into smaller ones that are easier to manage.

A programmer may define their own small sub-programs that can be 'called' (run) from anywhere else in the program using a single instruction...and as many times as necessary.

There are two types of user-defined sub-program ...

  1. Subroutine - which performs a specific task...eg draws a box on screen
  2. Function - which returns an answer...eg a mathematical calculation

Every subroutine has a name. The subroutine can be called by using its name.

 

 

Another good programming practice is to put subroutines and functions into a 'standard module'.

Standard modules are really libraries of subroutines and functions which may be used in any program. The advantages of this are ...

  • time is saved as the programmer does not have to write the same modules again.
  • the subroutines have already been tested so they will work and there is no need to test them again.
  • programs will end up being shorter and therefore easier to modify.