Procedures and Functions
Procedures and Functions
The procedure can then be called in the main part of the program as many times as is required in the
following way:
Instead of calling them procedures, different terminology is used by each programming language.
Procedures are known as:
It is often useful to pass a value to a procedure that can be used to modify the action(s) taken. For example,
to decide how many stars would be output. This is done by passing an argument when the procedure is
called to be used as a parameter by the procedure.
Procedures with parameters
Here is an example of how a procedure with parameters can be defined in pseudocode.
We can add parameters to a procedure:
Procedure with parameters are called like this – in this case to print seven stars:
Or:
A procedure call must match the procedure definition. This means that when a procedure is defined with
parameters, the arguments in the procedure call should match the parameters in the procedure definition.
For IGCSE Computer Science the number of parameters used is limited to two.
Functions
A function is just like a procedure except it always returns a value. Just like a procedure it is defined once
and can be called many times within a program. Just like a procedure it can be defined with or without
parameters.
Unlike procedures, function calls are not standalone and instead can be made on the right-hand side of an
expression.
Instead of naming them functions, different terminology is used by some programming languages. Functions
are known as:
• fruitful functions in Python
• functions in VB
The keyword RETURN is used as one of the statements in a function to specify the value to be returned. This
is usually the last statement in the function definition.
For example, a function written in pseudocode to convert a temperature from Fahrenheit to Celsius:
Because a function returns a value, it can be called by assigning the return value directly into a variable as
follows:
Just like with a procedure, a function call must match the function definition. When a function is defined
with parameters, the arguments in the function call should match the parameters in the procedure
definition. For IGCSE Computer Science the number of parameters used is limited to two.
When procedures and functions are defined, the first statement in the definition is a header, which contains:
The final line is an error because the main program has tried to access the variable Number3, which is local
to the procedure.