How to use subprograms to produce structured code
subprogramA small program that is written within a main program. are small programSequences of instructions for a computer. that are written within a larger, main program. The purpose of a subprogram is to perform a specific task. This task may need to be done more than once at various points in the main program.
There are two types of subprogram:
- procedureA section of computer code that performs a specific task.
- functionA section of code that, when programming, can be called by another part of the program with the purpose of returning one single value.
Benefits of using subprograms
- Subprograms are usually small in size, meaning they are much easier to write, test and debugThe process of finding and correcting programming errors. . They are also easy for someone else to understand.
- Subprograms can be saved separately as modules and used again in other programs. This saves time because the programmer can use code that has already been written, tested and debugged.
- A subprogram may be used repeatedly at various points in the main program. However, the code only has to be written once, resulting in shorter programs.
Procedures
A procedure is a subprogram that performs a specific task. When the task is complete, the subprogram ends and the main program continues from where it left off.
Example
A procedure may be written to reset all the values of an array to zero, or to clear a screen.
A procedure is created using the following syntaxRules governing how to write statements in a programming language.:
procedure identifier (value to be passed) procedure code
endprocedureThis procedure would clear the screen by printing x blank lines:
procedure clear_screen(x) for i = 1 to x: print(" ")
endprocedureA procedure is run by calling it. To call it, a programmer uses the procedure name and includes any values that the procedure needs, for example:
clear_screen(5)This would print five blank lines on the screen.
Functions
A function works in the same way as a procedure, except that it manipulates dataUnits of information. In computing there can be different data types, including integers, characters and Boolean. Data is often acted on by instructions. and returns a result back to the main program.
Example
A function might be written to turn Fahrenheit into Celsius:
function f_to_c(temperature_in_f) temperature_in_c= (temperature_in_f –32) * 5/9 return temperature_in_c
endfunctionA function is run by calling it. To call it, a programmer uses the function's identifierA name given to a part of a program, such as a variable, constant, function, procedure or module., the value to be passed into the function, and a variable for the function to return a value into.
Example
celsius = f_to_c(32)This would result in the value of Celsius being zero.
Built-in functions
Many languages include built-in, ready-made functions:
- int - converts stringA sequence of characters often stored as a variable in a computer program. These characters can include numbers, letters and symbols. or floating pointA data value in computer programming used to denote decimal numbers. into integerA whole number - this is one data type used to define numbers in a computer program. Integers can be unsigned (represent positive numbers) or signed (represent negative or positive numbers).
- str - converts a number into a string
- asc - finds the ASCIIAmerican Standard Code for Information Interchange. A 7-bit character set used for representing English keyboard characters. number of a character
Additionally, some languages allow functions to be added in from external files called libraryIn computing, a collection of functions stored in a separate file.. Libraries contain pre-written, tested functions that extend the functionality of a language.