Types of subroutines - procedures and functions
There are two types of subroutine:
- 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.
Procedures
A procedure is a subroutine that performs a specific task. When the task is complete, the subroutine ends and the main programSequences of instructions for a computer. continues from where it left off. For example, a procedure may be written to reset all the values of an arrayA set of data values of the same type, stored in a sequence in a computer program. Also known as a list. to zero, or to clear a screen.
A procedure is created using the following 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. syntaxRules governing how to write statements in a programming language.:
SUBROUTINE identifier (parameters)
procedure code
ENDSUBROUTINE
This procedure would clear the screen by printing x
blank lines:
SUBROUTINE clear_screen(lines)
FOR count <- 1 TO lines
OUTPUT " "
ENDFOR
ENDSUBROUTINE
A procedure is run by calling it. To call it, a programmer uses the procedure name and includes any parameterIn computer programming, a parameter is a value that is passed into a function or procedure. (values) that the procedure needs, for example:
clear_screen(5)
This would print five blank lines on the screen.
Functions
A functionA section of code that, when programming, can be called by another part of the program with the purpose of returning one single value. 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.
For example, a function might be written to turn Fahrenheit into Celsius:
SUBROUTINE f_TO_c(temperature_in_f)
temperature_in_c 鈫 (temperature_in_f 鈥32) * 5/9
RETURN temperature_in_c
ENDSUBROUTINE
A 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, for example:
celsius 鈫 f_to_c(32)
This would result in the value of Celsius being zero.
Note: AQA pseudo-code does not use a different keyword to show the difference between a procedure and a function. Instead, it adds a RETURN statementThe smallest element of a programming language which expresses an action to be carried out. to show that a subroutine is a function.
Local variables
A local variable is a variable that is used in one specific section of code, such as within a subroutine. Outside of the subroutine, the variable cannot be accessed or changed.
This subroutine, which prints 鈥楬ello!鈥 5 times on the screen contains a local variable, lines:
SUBROUTINE print_hello() lines = 5 FOR count <- 1 TO lines
OUTPUT "Hello!"
ENDFOR
ENDSUBROUTINE
Outside of the subroutine, it鈥檚 as if the variable does not exist. Its use, or scope, is limited to the subroutine only.
Because a local variable is confined to the subroutine, a programmer can reuse the variable name again in other parts of the program. For example, the same variable name could be used in several subroutines, meaning the programmer does not have to worry about always using a different variable name.