pascal:function
Un article de Polydoc.
Sommaire |
[modifier] Synopsis
function MaFonction (MaVariable : type ) : type ; var MesVariablesLocales : types ; begin { commandes } MaFonction := ValeurDeRetour ; End ;
[modifier] Description
La fonction MaFonction est déclarée avec la variable MaVariable attendue en paramètre du type Type.
Des variables locales MesVariablesLocales sont déclarées par le déclarateur var.
Les commandes sont écrites dans le bloc de la fonction délimité par begin et end.
La fonction retourne la valeur de la variable ValeurDeRetour.
[modifier] Notes
Pour appeler la fonction il suffit d'écrire le code suivant:
function somme(x,y: integer): integer; var calcul: integer; begin calcul := x + y; return calcul; end; resultat := somme(2,3); writeln('La somme de 2 et 3 fait :', resultat); { Affiche : La somme de 2 et 3 fait 5 }
[modifier] Exemples
type tableau = array[1..5] of integer; { Fonction qui tri un tableau de 5 cases dans l'ordre croissant } function tri(tab: tableau): tableau; var save,k,i: integer; begin k := 5; repeat for i:=1 to k do begin if (tab[i] > tab [i+1]) begin save := tab[i+1]; tab[i+1] := tab[i]; tab[i] := save; end; k := k - 1; until (k = 1); end; { Résultat: pour un tableau d'éléments 0,1,3,4,2 la fonction retourne le tableau 0,1,2,3,4 }

