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 array 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. syntax:
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 parameters (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.
For example, a function might be written to turn Fahrenheit into Celsius:
A function is run by calling it. To call it, a programmer uses the function's identifier, 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.