Académique Documents
Professionnel Documents
Culture Documents
Sous-algorithme
(Fonctions et procédures)
1
Prof: R. EL AYACHI
Définition
3
Prof: R. EL AYACHI
Procédure
Déclaration:
Procédure Nom_Procédure(Nom_Argum:Type_Argum;….);
Variable Nom_Var: Type;
… Nom de la
procédure
Début
instruction1;
Variables
instruction2; locale
…
Fin;
Corps de la
procédure
4
Prof: R. EL AYACHI
Procédure
Appel:
L’appel de la procédure s’effectue (au moment souhaité) par
son nom et éventuellement ses paramètres. Cet appel
déclenche l’exécution des instructions de la procédure.
Nom_Procédure(Param1;Param2;….);
5
Prof: R. EL AYACHI
Procédure
6
Prof: R. EL AYACHI
Fonction
Déclaration:
Fonction Nom_Fonction(Nom_Argum:Type_Argum;….):Type_Ret;
Variable Nom_Var: Type; Nom de la
fonction
…
Début
Type du résultat
Variables
instruction1; à retourner
locale
instruction2;
… Corps de la
fonction
Nom_FonctionRésultat;
Fin;
7
Prof: R. EL AYACHI
Fonction
Appel:
L’appel de la fonction s’effectue (au moment souhaité) par
son nom, ses paramètres et en affectant le résultat retourné
à une variable. Cet appel déclenche l’exécution des
instructions de la fonction.
Nom_Variable Nom_Fonction(Param1;Param2;….);
Variable stockant le
résultat retourné Liste des
paramètres
Nom de la
fonction
8
Prof: R. EL AYACHI
Fonction
9
Prof: R. EL AYACHI
Exercice
On veut résoudre une équation du premier
degré en utilisant la notion des sous-
algorithmes.
Ecrire deux sous-algorithmes comme suit:
• Une fonction lecture(): saisir et
retourner un réel
• Une procédure solution(float,float):
résoudre une équation du premier degré.
10
Prof: R. EL AYACHI
Exercice (Solution)
Fonction lecture():
Fonction lecture( ):Réel;
Variable val: Réel;
Début
Ecrire(‘Entrer une valeur’);
Lire(val);
lectureval;
Fin;
11
Prof: R. EL AYACHI
Exercice (Solution)
Procédure solution():
Procédure solution(a: Réel, b: Réel);
Variable x: Réel;
Début
Si a <> 0 alors
x-b/a; Ecrire(x);
Sinon
Si b=0 alors Ecrire(‘IR’);
Sinon Ecrire(‘Ensemble vide’);
Finsi
Finsi
Fin;
12
Prof: R. EL AYACHI
Exercice (Solution)
Conception:
Algorithme Equation; Si a <> 0 alors
Variable A,B: Réel; x-b/a; Ecrire(x);
Fonction lecture( ):Réel; Sinon
Variable val: Réel; Si b=0 alors Ecrire(‘IR’);
Début SinonEcrire(‘Ensemble vide’);
Ecrire(‘Entrer une valeur’); Finsi
Lire(val); Finsi
lectureval; Fin;
Fin; Début
Procédure solution(a:Réel, b:Réel); A lecture();
Variable x: Réel; B lecture();
Début solution(A,B);
Fin
13
Prof: R. EL AYACHI
Récursivité
Fonction récursive:
C’est une fonction qui contient un appel à elle même.
Fonction Nom_Fonction(Nom_Argum:Type_Argum;….):Type_Ret;
Variable Nom_Var: Type;
… Appel de la fonction
Début elle même
instruction1;
instruction2;
…
Nom_FonctionNom_Fonction(param,…);
Fin; 14
Prof: R. EL AYACHI
Récursivité
Fonction Factoriel(N:Entier;….):Entier;
Début
Si N = 1 alors Factoriel 1;
Sinon Factoriel N*Factoriel(N-1);
Finsi
Fin;
15
Prof: R. EL AYACHI