0% found this document useful (0 votes)
26 views9 pages

Chapitre5 en

This document discusses parameterized actions (procedures and functions) in programming. It defines parameterized actions as subalgorithms that take data as input parameters and produce results. Using parameterized actions makes algorithms more readable, reusable, and editable. The document outlines the syntax for declaring procedures and functions. It distinguishes between value (input) parameters and variable (output) parameters. Value parameters pass values into procedures, while variable parameters pass the address of variables, allowing the procedures to directly modify the variables.

Uploaded by

Bhan aa3
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)
26 views9 pages

Chapitre5 en

This document discusses parameterized actions (procedures and functions) in programming. It defines parameterized actions as subalgorithms that take data as input parameters and produce results. Using parameterized actions makes algorithms more readable, reusable, and editable. The document outlines the syntax for declaring procedures and functions. It distinguishes between value (input) parameters and variable (output) parameters. Value parameters pass values into procedures, while variable parameters pass the address of variables, allowing the procedures to directly modify the variables.

Uploaded by

Bhan aa3
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/ 9

CHAPITRE 5 : Parameterized Actions (Procedures and Functions)

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.

Data Parameterized actions Results

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.

II Declaring a Parameterized Action:

The parameterized action is syntactically made up of:


1. The header: allows you to give a name (an identifier) to the parameterized action as
well as the specification of the parameters.
2. Body: Has a part declaring objects specific to the action, which can be constants,
types, or variables. It also has an action part.

Il existe deux types d’actions paramétrées : les Procédures et les Fonctions.

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:

The syntax of a procedure statement is as follows:

PROCEDURE < identifier>;


<Statement of Procedure Part>
Begin
<instructions>
End;

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:

Procedure <identifier> (list of parameters);


<Declarations>
Begin
<instructions>
End ;

Example of reusability:

If we want to draw 3 lines of stars


********
********
********
One treatment and 3 more star lines
********
********
********
Another treatment and 3 more star lines
********
********
********
Instead of repeating the same treatment several times, we can write a procedure to draw 3 star
lines and simply use this action as many times as necessary.

III.3 The structure of an algorithm using a procedure:

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 i1 To 3 Do
For j 1 To 10 Do Write(‘ * ‘) Done;
Writeln;
Done ;
End ;
Begin
Star3 ;
/*Another treatment ……….*/
Star3;
/* Another treatment……….*/
Star3 ;
End.

Au lieu de répéter le bloc d’instruction 3 fois on répète l’appel de procédure 3 fois.

III.4 Global Variables and Local Variables:

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.

III.5 The VALUE and VARIABLE parameters:

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 i1 To 3 Do
For j 1 To X Do Write(C); Done;
Writeln ;
Done ;
Xx+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
yy+10;
Write (y); {ici y=15}
End;
Begin
X5;
Test(X);
Write(X); {ici X=5}
End.

b) VARIABLE Parameters ( Output Parameter):

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.

If we replace the procedure header in the previous example with:

Procedure Test (I-O/ y :Integer) ;

When the "Test" procedure is called, the procedure is provided with the address of the field
containing the value of X.

X 44100 Global Variables Area

Y 44100 Parameters and Local Variables area

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
yy+4; 10
Write (y) ; 10
End ;

Procedure passage_Variable (I-O/ y :Integer);


Begin
Write (y); 2
yy+5; y=7
Write (y) ; 7
End ;
Begin
x2;
Write (x); 2
passage_Valeur (3*x);
Write(x); /*affiche 2 */ 2
passage_Variable(x);
Write (x); /*affiche 7*/ 7
End.

III.6 The principle of appeal of procedures:

The call for procedures is schematized below:

Algorithme principal la procédure A


<partie déclaration>
Begin Begin
-----
------
---- End ;
appeler la procédure A ;
-----
-----
----- la procédure B
appeler la procedure B ;
----- Begin
----
---- End ;
End.

• Imbrication des appels :

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.

III.7 Nesting of parameterized actions:

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;

B can call A but not the other way around.

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;

The main algorithm can only call A.

***************************************

37

You might also like