What is a sub-program?
Sub-programs are self contained sections of code, stored under an identifier that is used to call them when you want them to be run. They may or may not take one or more
parameters, which are values passed to the sub-program from the main program. There are two types of subroutines:
Advantages of sub-programs
They are small and generally perform just one function so are easier to write, test and debug. They can be called and run as often as you like in the main program without the
need for more code reducing the time taken to write programs. Sub-programs can be saved as modules and imported into other projects saving time by using tried and tested code.
By using sub-programs large programs can be written by different programmers working on their own sub-programs.
Procedures VS Functions
A function executes some code and returns one or more values to the main program before returning control to the main program. This means it must be assigned to a variable/s
to store that value/s that are returned when it is executed.
A procedure returns no value, it simply executes it's code before returning control to the main program. This
means it is called without assignment. In the examples below, add is a function as it returns a result, where as say hello is a procedure. The add function takes two paramaters
and the say_hello procedure takes one.
Sub-program syntax
The syntax and example are shown for a function and a subroutine below.
Local and global variables
The scope of a variable refers to where in the program it can be accessed from. A variable declared in the main program has global scope, meaning it can be accessed in any part
of the program including sub-programs. A variable defined in a sub-program exists only within the sub-program in which it is created.
It can be tempting to use global values throughout so you can access them from any part of the program. If one variable is affected by several sub-programs, there may be
unexpected effects and errors in one sub-program may have knock on effects on another. Global variables have to be in RAM throughout a program's execution. Large programs can have
thousands of variables leading to naming issues.
Parameter passing
Parameter passing allows us to access and update variables local to the main program from inside sub-programs, without the need for global variables. When we define parameters
in a sub-program, we are creating a local copy of a variable in the main program. This allows us to create a completely self-contained section of code that is not affected by other
parts of the program. Programmers can use any variable they like in local scope without concern of a conflict elsewhere in the program. If a value changes during the execution of
a sub-program, then we can return it's updated value to the main program at the end.
In the example below you can see that while the value passed as a parameter may have the same name as the placeholder, it doesn't have to so both methods of calling this function
are fine.
Built in sub-programs
There are a number of built-in sub-programs in most languages and more that can be imported as modules. Built-in functions we have already seen include upper( ), lower( ),
int( ), real( ) and str( ).
Random number generation
In OCR reference language you can use the random command with two integers and get a random integer between those values inclusive e.g. number = random(1,10) would assign a
random number between 1 and 10 to the variable number. If real values are passed, then a real value between the two parameters will be randomly generated. In some languages, like
Python, the random module must be imported separately.