0% found this document useful (0 votes)
16 views8 pages

Procedures and Functions

Uploaded by

mimouni2am
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views8 pages

Procedures and Functions

Uploaded by

mimouni2am
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Course

Algorithms and Data Structures I

ENSM
2023 – 2024

Chapter IV: Procedures and functions.

1. Introduction

Let’s consider the following problem:

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:

The verification of membership in a set results in a search algorithm. So, we have to


look for x in the given arrays. Let's apply the algorithm of sequential search in an array
of integers.

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;

// Reading the dimension of T1


repeat
print("Please enter the dimension of the first array:”);
read(d1);
until (d1>0 & d1<N);

// Reading the dimension of T2


repeat
print("Please enter the dimension of the second array:”);
read(d2);
until (d2>0 & d2<N);

// Reading the elements of T1


for i=0 to d1 step 1 do
read(T1[i]);
endfor

// Reading the elements of T2


for i=0 to d2 step 1 do
read(T2[i]);
endfor

// Reading the value of x


read(x);

// 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

// Display search results


if (b1 ½ b2) then print(“The value of x belong to T1 or T2”);
else print(“x does not exist in T1 nor in T2”);
endif

end

What comments can be made on this solution?

1. This solution is unnecessarily long: A lot of repetitions of the same instructions;

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).

ð A suggestion for a better solution:


To be able to reuse the same piece of instructions with different variables and values, a solution would
be to associate a name and a set of parameters to the groups of actions that are repeated.

Let’s consider that:


• SeqSearch (x, T1, d1) performs the search for x in the array T1 of dimension d1 and
SeqSearch (x, T2, d2) performs the search for x in the array T2 of size d2.
• ReadArray(T1, d1) and ReadArray(T2, d2) the actions for reading arrays T1 and T2.

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;

// Reading the dimension of T1


repeat
print("Please enter the dimension of the first array:”);
read(d1);
until (d1>0 & d1<N);

// Reading the dimension of T2


repeat
print("Please enter the dimension of the second array:”);
read(d2);
until (d2>0 & d2<N);

// Reading the elements of T1 and T2


ReadArray(T1, d1);
ReadArray(T2, d2);

// Reading the value of x


read(x);

// Search for x in T1 and in T2


b1 ¬ SeqSearch (x, T1, d1);
B2 ¬ SeqSearch (x, T2, d2);

// Display search results


if (b1 ½ b2) then output(“The value of x belong to T1 or T2”);
else output(“x does not exist in T1 nor in T2”);
endif

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.

2. Procedures and functions


The resolution of a complex problem requires its decomposition into a set of sub-problems of lesser
difficulty. Each sub-problem is structured into a sequence of actions, that is a procedure or a function,
and that is identified by a name and communicates with the rest of the algorithm through parameters.

ð Defining a procedure or a function consists in writing the algorithm of this action, by


providing its formal parameters.

ð 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.

2.1 Declaration of a procedure


procedure procedure_name (parameter1, ... , parameterk);

start
<declaration of local variables>;

<action 1>;
<action 2>;
...
<action m>;
endprocedure
5
2.2 Declaration of a function

function function_name (parameter1, ... , parameterk): returnType;

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:

transmission_mode / parameter_name : parameter_type

The type of the parameter (parameter_type): integer, real, character, boolean...

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.

2.4 Local variables in a function or procedure


A local variable is a variable that can only be used inside the function/procedure. It is thus declared
inside the declaration of the function/procedure. By default, local variables are temporary. When
exiting the procedure/function, local variables are therefore lost.

6
2.5 Examples

2.5.1 Procedure that “writes” the minimum of two integers

procedure minimumProc (E/ x: integer, E/ y: integer);


start
if (x < y) then output(x);
else output(y);
endif
endprocedure

2.5.2 Function that computes the minimum of two integers

function minimumFunc (E/ x: integer, E/ y: integer): integer;


start
m: integer;
if (x < y) then m ¬ x;
else m ¬ y;
endif
return(m);
endfunction

7
function minimumFunc (E/ x: integer, E/ y: integer): integer;
start
if (x < y) then return(x);
else return(y);
endif
endfunction

2.5.3 How to call the procedure “minimumProc”?

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

2.5.4 How to call the function “minimumFunc”?

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

You might also like