ADS - II Course Chapter - 2 Modularity (Func Proc)
ADS - II Course Chapter - 2 Modularity (Func Proc)
Chapter 2
Subprograms:
Functions and Procedures
MI-L1-UEF121 : Algorithms and Data Structures II
Noureddine AZZOUZA
1
Course
Topics
1. Modularity
2. Passing parameters
4. Functions
5. Procedures
2
ASD II Noureddine AZZOUZA
Modularity
3
Introduction Algorithm Calcul_Combinaison ;
Var n,p,c : integer ;
fact_n, fact_p, fact_np: integer;
Problem Begin
//les entrées
Read (n,p);
fact_p = 1;
For i := 1 à p Do
fact_p := fact_p * i;
fact_np = 1;
For i := 1 à (n-p) Do
fact_np := fact_np * i;
c := fact_n/(fact_p*fact_np);
//les sorties
Write (‘le nombre de combinaisons=‘,c);
End.
4
ASD II Noureddine AZZOUZA
Introduction
Problem
fact_n = 1;
Repeating For i := 1 to n Do How to
fact_n := fact_n * i;
the write the
factorial fact_p = 1; solution
For i := 1 to p Do
calculation only once?
Modularity
fact_p := fact_p * i;
Organize
fact_np = 1;
For i := 1 to n-p Do the code?
fact_np := fact_np * i;
Modules / Subprograms
5
ASD II Noureddine AZZOUZA
Subprograms
Definition
A subprogram or module is a set of instructions with a well-defined
interface that performs a specific task.
6
ASD II Noureddine AZZOUZA
Subprograms
Structure
Interface Results
Data (inputs)
(outputs)
Type that depends
on the output
Type of Subprograms
Modularity
0 to n inputs name_sub_programs 0 to n
outputs
Role of Sub-Program
unique and meaningful Role that indicates what
name that is used in the exactly the subroutine
declaration and appeal does
7
ASD II Noureddine AZZOUZA
Subprograms
Types
Depending on the number and type of outputs, there are two (02) types
of Subprograms (modules):
1. Function : When the module returns a single (1) result and this
result is elementary (basic) type data, example:
Modularity
Qualities
In order to decide whether a sequence of instructions deserves to be
designed in the form of a subroutine or module, the following qualities
must be checked:
Modularity
3. Simplicity: keep your code readable and design a module that meets a
specific task.
9
ASD II Noureddine AZZOUZA
Modular Approach
Definitions
Modularity: “It is a way of thinking aimed at building algorithms starting
from a very general level and gradually detailing each treatment, until
arriving at the lowest level of description.”
10
ASD II Noureddine AZZOUZA
Modular Approach
Objectifs / Goals
Cut (divide) a complex problem into simple sub-problems which will be
solved separately.
Propose a solution to a (sub)problem once and only once
Sous
Sous
P11
S11
Prb
Sol
Sous
Sous
Prb
Problème complexe P
Sol
P1
S1
Sous
Sous
P12
S12
Solution finale S
Prb
Sol
Sous
Sous
Prb
Sol
P2
S2
Sous Sous
Sous Sous
P31
S31
Prb
Sol
Sous
Sous
Prb
P32
S32
Sol
Prb
P3
S3
Sol
Sous
Sous
P32
S32
Prb
Sol
11
ASD II Noureddine AZZOUZA
Modular Approach
1st STEP:
• Modular Cutting/Split 3rd STEP:
Understanding
• Construction of Realization
the problem Modules
12
ASD II Noureddine AZZOUZA
Modular Approach
Modular Breaking/Splitting
Break the problem into coherent modules:
13
ASD II Noureddine AZZOUZA
Modular Approach
Build Modules
To build a module, we start with its description:
Avantages / Benefits
Cutting into coherent modules is done using a Top-Down Approach
Simplifies design
15
ASD II Noureddine AZZOUZA
Communication & Calls
–
Modules Module_A
Begin
-
-
Call Module_A(arg1, arg2, ..., argn); Called Module
-
-
End.
Calling module
The variables used when calling (using) a module are called Effective parameters or
arguments. They replace the formal parameters during the call.
Example : les arguments (ou paramètre effectifs) du module « Module_A » utilisés
dans son appel à l’algorithme principale sont : arg1, arg2, ..., argn
Passing by Variable (by Reference): any modification of the content of the formal parameter
automatically results in the modification of the effective parameter.
formal parameters passed by variable are preceded by the keyword VAR in the header of
the
18
ASD II Noureddine AZZOUZA
Passing Parameters
Passing by Value
Params Passing Modes
:
Function Surface(long, larg:real) real; :
Function Surface(long, larg:real) real;
Var S: integer; Var S: integer;
Module Begin Begin
S := long * larg; long := x; larg := y;
Surface := S; S := long * larg;
End; Surface := S;
19 End;
ASD II Noureddine AZZOUZA
Passing Parameters
Passing by Variable
Params Passing Modes
In passing by variable (or reference) the parameter itself becomes the argument,
that is, the parameter becomes an alias of the argument.
Can only be linked to variables..
Example : subroutine (procedure) which swaps (exchanges) the values of two variables..
Echange (a, b); Call
équivalent à
Passing by Value: is adopted when we want the module to return the same value that the
parameter had at the input, or the parameter is not used in other modules (useless to
find the final result).
Passage by Variable (by Reference): is adopted when the input parameter is modified
during the execution of a subroutine and it is the modified content of the parameter that
we want.
21
Passage by Variable Passing by Value
ASD II Noureddine AZZOUZA
Passing Parameters
Notes
Params Passing Modes
It is recommended to use:
22
ASD II Noureddine AZZOUZA
Local and Global
Local Variables: which are defined in a module and which can only
be manipulated in this module.
Var A, B, X: real;
Module_1 Module_Appelant , Module_1,
A,B
Var X: integer; Module_2, Module_3
T: booléen; T Module_1, Module_2
Module_2
C Module_2
Var C: integer;
X:déclaré au
Module_Appelant
Module_Appelant
Module_3
Var X, Y, Z: integer; X:déclaré au
Module_1, Module_2
Module_1
Begin X:déclaré au
- Module_3
Module_3
-
End; Y, Z Module_3
24
ASD II Noureddine AZZOUZA
Functions
25
Functions
FUNCTION
Functions
Description of a fuonction
26
ASD II Noureddine AZZOUZA
Functions
Body Begin
-
-
- 𝑻𝒓𝒂𝒊𝒕𝒆𝒎𝒆𝒏𝒕𝒔
-
-
- fonction_name := Result;
End;
27
ASD II Noureddine AZZOUZA
Functions
28
ASD II Noureddine AZZOUZA
Functions
Calls
Calling a function can be used as:
Expression in an assignment,
Operand in a condition
Argument in a procedure or function call
Functions
Examples:
X := Prime (a)
if Prime (a) = True then write ( a, ‘is prime’)
Res := Prime (Fact(n))
29
ASD II Noureddine AZZOUZA
Example: Calcul of Combinaisons
30
ASD II Noureddine AZZOUZA
Example: Calcul of Combinaisons
F := F * i;
Fact := F;
End;
31
ASD II Noureddine AZZOUZA
Example: Calcul of Combinaisons
c := Comb(x,y);
Write (‘Le nombre de combinaison = ‘, c);
End;
32
ASD II Noureddine AZZOUZA
Function declaration
PASCAL C
FUNCTION nom_fonction (Input parameters): type_retour; type_retour nom_fonction (Input parameters);
Programmation C / PASCAL
33
ASD II Noureddine AZZOUZA
Function declaration
PASCAL
Programmation C / PASCAL
In this example:
A Call to a Fact function (line 17)
n: parameter (formal parameter) (line 5)
x: argument (effective parameter)(line 17)
34
ASD II Noureddine AZZOUZA
Function declaration
C
Programmation C / PASCAL
35
ASD II Noureddine AZZOUZA
Function declaration
C
Programmation C / PASCAL
36
ASD II Noureddine AZZOUZA
Procedures
37
Procedures
PROCEDURE
Description of a procedure
38
ASD II Noureddine AZZOUZA
Procedures
Var – 𝒍𝒐𝒄𝒂𝒍𝒆𝒔
Body Begin
-
- 𝑻𝒓𝒂𝒊𝒕𝒆𝒎𝒆𝒏𝒕𝒔
-
-
-
End;
39
ASD II Noureddine AZZOUZA
Procedures
40
ASD II Noureddine AZZOUZA
Procedures
Calls
Calling a procedure is a primitive action. It is composed
of the name of the procedure followed in parentheses by the list of effec
tive input and output parameters separated by commas.
Procedures
As for functions, the number, order, and type of the effective paramete
rs must be identical to those of the formal parameters.
Examples:
remplir_tab (n, T)
echange (x, y)
41
ASD II Noureddine AZZOUZA
Example: Convert a time T (in seconds) to hours, minutes and seconds
convert_temps (T, h, m, s)
h : integer
1. On divise T sur 360 : le quotient est h T: integer convert_temps m : integer
Functions
s : integer
2. Le reste de cette division est divisé sur 60
a. Le quotient est m Role : Converts a time T (in seconds) into
b. Le reste est s h hours, m minutes and s seconds
42
ASD II Noureddine AZZOUZA
Example: Convert a time T (in seconds) to hours, minutes and seconds
R := T MOD 3600;
m := R DIV 60;
s := R MOD 60;
End;
43
ASD II Noureddine AZZOUZA
Example: Convert a time T (in seconds) to hours, minutes and seconds
convert_temps(A, x, y, z);
Write (A,'=',x,'heures et ',y,' minutes et ',z,'secondes');
End;
44
ASD II Noureddine AZZOUZA
Exemple: Convertir un temps T (en secondes) en heures, minutes et secondes
PASCAL C
PROCEDURE nom_procedure (Input/output parameters); void nom_fonction (Input/output parameters);
Programmation C / PASCAL
45
ASD II Noureddine AZZOUZA
Declaration of a Procedure
PASCAL
Programmation C / PASCAL
C
Programmation C / PASCAL
47
ASD II Noureddine AZZOUZA