Chapitre5 en
Chapitre5 en
I. Introduction :
The purpose of this chapter is to present a very powerful way to structure an algorithm. These
are procedures and functions, also known as parameterized actions.
Indeed, a problem can be broken down into several sub-problems, and each sub-problem
gives rise to a sub-algorithm. The initial problem solving algorithm is an ordered sequence of
the sub-algorithms for solving all the sub-problems (the different algorithms are relatively
independent).
Definition of a parameterized action : A parameterized action is a subalgorithm that contains
a set of instructions to which we associate a name (IDF) which, based on certain data,
provides us with a certain number of results. These data and results are called Arguments or
Parameters.
Interest of A. P:
a)Readability: if the algorithm is too long⇒ think about breaking it down into A.P.
b)Reusability: A. P is set once and can be used multiple times.
c)Editability : just change the A. P without touching the main algorithm.
This structure makes it much easier to understand an algorithm, just as the subdivision of a
book into chapters and paragraphs simplifies the reading.
III Procedures:
III.1 Definition:
A procedure allows us to name a set of statements. From the main algorithm, all you have to
do is invoke this name, and the instructions of the procedure will be executed at that moment.
29
III.2 Syntax:
Declaring a procedure is therefore similar to declaring a variable, but here we are declaring an
action, whereas for a variable we are declaring a piece of data.
Remarks:
1. The procedure is placed before the main algorithm.
2. In the case where parameters are associated with the procedure, then its declaration
becomes:
Example of reusability:
30
Header ALGORITHM
En-Tête WERE....
Declaring Variables
EEeenN
Statement of Procedure …
Declaring Var…
Procedures
procedure’s Begin
Variables …..
End ;
Body of the algorithm
principal Begin
….
End.
In an algorithm, we go through the variable declarations and then move on to the main
algorithm. The names of the procedures already make it possible to know what is going on,
without needing to know the details of how. Then we can look at what these procedures do.
Exemple :
Algorithm Star ;
Var i, j : Integer ;
Procedure Star3 ;
Begin
For i1 To 3 Do
For j 1 To 10 Do Write(‘ * ‘) Done;
Writeln;
Done ;
End ;
Begin
Star3 ;
/*Another treatment ……….*/
Star3;
/* Another treatment……….*/
Star3 ;
End.
1. Global variables:
When a variable is used by the main algorithm, or by multiple procedures, we need to declare
it at the top of the algorithm. These variables are called global variables.
It is often advisable to reduce the use of global variables in order to limit possible side effects
(see Section IV).
a) Local variables:
31
Variables declared within a procedure are called local variables. A variable is declared locally
because we only need them in the procedure and they can only be accessed by the procedure.
Note: Local variables are only usable in the procedure where they are declared, and they are
created the moment we enter the procedure and then destroyed when we exit the procedure, so
their lifetime is that of the procedure.
Example:
Algorithm global_local ;
Var V1 :Integer ;
Procedure P ;
Var V2 :Integer;
Begin
V2 1000 ;
Write(V2) ;
End;
Begin
V1 3 ; P ; Write(V1) ;
End.
Algorithm Variables ;
Var
Val : Integer ;
Procedure P ;
Var
Val : Integer ;
Begin
Val 1000 ;
Write (‘dans la procédure Val= ‘, Val) ;
End;
Begin {Main Algorithm}
Val 3 ;
P ; {call of the procedure}
Write (‘after The Call for Procedure Val= ‘, Val) ;
End.
Remarks:
1. The major advantage of declaring local variables is the saving of memory space.
2. The Main Algorithm can't use local variables (they don't exist when we're in the main
algorithm).
3. Even if we have a local variable that has the same name as a global variable, it's not
the same area so their values are distinct.
If you want to draw three lines ten times the $ character, you can't (directly) use the star3
procedure because it's fixed since it doesn't contain parameters.
In the remainder of this chapter, we are concerned with the procedural statement containing a
list of parameters after the name of the procedure, as already explained in the remark in
paragraph III.2
32
a) VALUE parameter (Input) :
Suppose we want to draw three lines containing ten times the character *, then three lines of
ten times the $ character, then three lines of ten times the # character. Instead of writing
three procedures, there is a more economical way, which is to introduce parameters.
Definition:
To write general procedures, we use parameters. These parameters provide the procedure
with the value to be used to initialize the variables, which are then called VALUE parameters,
corresponding to the value transmission mode still called, the Input mode.
To declare the value parameter, simply place the Input keyword abbreviated as (I /) in front
of its name.
In fact, we can also call the procedure using either constants, variables, or expressions as a
value parameter (e.g. the Pascal language will evaluate the expression and initialize the
parameter of the procedure with this value).
Example:
We want to display 3 lines of 10 ('*') then 3 lines of M ('$') and then 3 lines of M+10 ('#');
Algorithm Parametres ;
Var
M : Integer ;
Procedure dessin (I/ C :character ; I/ X :Integer) ;
Var i, j :Integer ;
Begin
For i1 To 3 Do
For j 1 To X Do Write(C); Done;
Writeln ;
Done ;
Xx+10;
End ;
Begin
Dessin(‘*’,10) ; {constante}
Read(M) ;
Dessin(‘$’,M) ; write(M) {Constant and Variable}
Dessin(‘#’,M+10) ; {Constant and Expression}
End.
Remarks:
1. In the body of the procedure, the parameters are used as if they were local variables.
That is, if the value of the value parameter changes in the procedure, this change is not
valid in the main algorithm.
2. When executing the procedure instructions, the initial values of these "special local
variables" are the values of the variables that are specified at the time of the call.
Example:
Algorithm parametre_valeur ;
Var
X : Integer ;
Procedure Test (I/ y: Integer);
Begin
33
yy+10;
Write (y); {ici y=15}
End;
Begin
X5;
Test(X);
Write(X); {ici X=5}
End.
According to the previous example, if you want to modify the value of X, i.e. display in the
main algorithm the value obtained in the procedure, you just have to declare this parameter in
Input/Output mode or in Output mode by placing the abbreviated keyword (I-O/) or (O/) in
front of its name.This type of parameter is called a variable parameter. It is, in fact, a
transmission by address that we will explain later.
When the "Test" procedure is called, the procedure is provided with the address of the field
containing the value of X.
This procedure then directly manipulates this variable. The parameter box will therefore
contain the address of this variable.
Because of this mechanism, variable parameters are also called per-address parameters.
Remarks:
1. The variables described in the procedure header are called FORMAL parameters.
2. The variables provided by the calling algorithm when the procedure is called the
EFFECTIVE or REELS parameters.
3. There must be an absolute correspondence in number, type, and order between the
formal parameters and the actual parameters.
Conclusion :
34
1. The Value parameter IMPORTS a value into the procedure (which the procedure uses
to perform its processing). I/
2. The Variable parameter IMPORTS and EXPORTS values. I-/
Exemple :
Algorithm Parametres ;
Var
x : Integer;
Procedure passage_Valeur (I / y : Integer) ;
Begin
Write (y); 6
yy+4; 10
Write (y) ; 10
End ;
Lors d’un appel imbriqué de procédures nous devons respecter les points suivants :
35
- Respect de transmission des paramètres.
- Respect de retour de l’action appelée vers l’action appelante.
- Chaque action paramétrée doit avoir un début et une fin bien déterminés.
- Une action paramétrée ne peut appeler que les actions qu’elle englobe directement et
celles du même niveau mais déclarés avant elle.
1-You can declare several actions parameterized one after the other or an action within
another action.
Var
Procedure A ;
Begin
….
End;
Procedure B ;
Begin
….
End;
2- If one action is inside another action, it is the one that is outside that calls for the action that
is inside.
Procedure A ;
Var….
Procedure B ;
Begin /*de B
….
End;
Begin { actions of A}
…. B; …
End;
A peut appeler B.
Procedure A ;
Var ….
Procedure B ;
Var…
Procédure C ;
Begin
….
End;
Begin { actions of B}
…C;…
End;
Procedure D ;
Begin
….B; …
End;
36
Begin { actions of A}
…. B; …D; …
End;
***************************************
37