Procedures and Functions
Procedures and Functions
ENSM
2023 – 2024
1. Introduction
Let T1 and T2 be two arrays, each has at most N integers (N<=20) and let x be a given integer. Write
the algorithm to find out if x Î (T1 v T2).
ð Solution:
From the statement, arrays have no more than 20 components, but they can have fewer.
So the algorithm must accept a dimension value (for each array) such as 0 < dimension
<= 20 and reject any dimension outside this range. This is the “filtering” part.
Then we have to read the two tables and apply the search in each of them to answer to
the question. This leads to the following algorithm:
1
algorithm SearchIn2Arrays_V1;
start
const N = 20;
T1: array[N] integer;
T2: array[N] integer;
i, x, d1, d2: integer;
b1, b2: boolean;
// Search for x in T1
b1 ¬ false;
i ¬ 0;
while (b1=false & i<d1) do
if (T1[i] = x) then b1 ¬ true;
else i ¬ i+1;
endif
endwhile
2
// Search for x in T2
b2 ¬ false;
i ¬ 0;
while (b2 = false & i<d2) do
if (T2[i] = x) then b2 ¬ true;
else i ¬ i+1;
endif
endwhile
end
2. Lines of code for the search in T1 are exactly the same as that of the search in T2, except
for the renaming of the variables. To deal with that, all we have to do is to give a name to
this group of actions and parameters to distinguish its use in both cases.
3. The same remark applies to instructions for reading the arrays; They differ only in the
name of the array and its dimension.
4. What would this algorithm be like if the question to be answered was to check if:
x Î (T1 v T2 v T3 v T4 v T5).
3
The previous algorithm becomes:
algorithm SearchIn2Arrays_V2;
start
const N = 20;
T1: array[N] integer;
T2: array[N] integer;
i, x, d1, d2: integer;
b1, b2: boolean;
end
4
It is therefore necessary to define ReadArray(T, size) which reads any array of integers T that has a
number of components equal to size. Similarly, it is necessary to define SeqSearch (val, T, size)
which checks whether or not an integer val exists in an array of integers T of size integer components.
ð Calling the procedure or the function with its parameters consists in using it at the places of
the algorithm with the effective parameters (the parameters must have the values with
which the process must actually be executed).
Functions and procedures are hence the basic building blocks of algorithms. They are small sections of
“code” that are used to perform a particular task, and they are used for two main reasons:
a. The first reason is that they can be used to avoid repetition of instructions within the
algorithm. If you have operations that are performed in various different parts of the
algorithm, then it makes sense to remove the repeated code and create a separate function
or procedure that can be called from those places instead. Not only will this reduce the size
of your program, but it will make the code easier to maintain, and it will remove the
possibility that some of the code segments are updated, but not others.
b. The second reason for careful use of functions and procedures is to help define a logical
structure for the algorithm program by breaking it down into a number of smaller modules
with obvious purposes.
start
<declaration of local variables>;
<action 1>;
<action 2>;
...
<action m>;
endprocedure
5
2.2 Declaration of a function
start
<declaration of local variables>;
<action 1>;
<action 2>;
...
<action m>;
return result;
endfunction
2.3 Parameters
In the declaration of a procedure or a function, each parameter is defined according to the following
scheme:
The mode of transmission (transmission_mode) informs about the mode of transmission of the
parameter, which can be: as Input (I), as Output (O) or as Input/Output (I/O).
• I: The parameter receives its value from the “outside”, that is from the algorithm that calls the
procedure or the function.
• O: The parameter will get a value from the inside the procedure or the function and it is
outputted to the algorithm that has called the procedure or the function.
• I/O: The parameter value is received from the outside, it can be modified within the procedure
or the function and it is then sent to the outside.
6
2.5 Examples
7
function minimumFunc (E/ x: integer, E/ y: integer): integer;
start
if (x < y) then return(x);
else return(y);
endif
endfunction
The call to the procedure “minimumProc” will be made in the calling algorithm (the main program for
example, or another procedure/function) with the known values of x and y, that are a and b in the
example bellow.
algorithm min_2integers;
start
a, b: integer;
read(a);
read(b);
minimumProc(a, b);
end
The call to the function “minimumFunc” will be made in the calling algorithm (the main program for
example, or another procedure/function) with the known values of x and y, that are a and b in the
example bellow.
algorithm min_2integers;
start
a, b, min: integer;
read(a);
read(b);
min ¬ minimumFunc(a, b);
print(min);
end