40
++- S5 … 1 M. BENJELLOUN : 2019-2020 U MONS Séance 5 Fonctions_suite

Présentation PowerPoint C++ : Transp 1er Bac · M. BENJELLOUN: 2019-2020 ++-S5 …6 UMONS Fonctions pourquoi ? #include using namespace std; void main() {int i,

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

++- S5 … 1M. BENJELLOUN : 2019-2020 UMONS

Séance 5

Fonctions_suite

++- S5 … 2M. BENJELLOUN : 2019-2020 UMONS

int i, S=0;

for (i = 0; i <N; i++)

S = S + i ; //S+= i;

cout<<" SOM="<<S<< endl;

void main() {

int i, S1=0, S2=0, S3=0;

for (i = 0; i <5; i++) {

S1 = S1 + i ;

}

cout<<" SOM="<<S1<< endl;

for (i = 0; i <10; i++) {

S2 = S2 + i ;

}

cout<<" SOM="<<S2<< endl;

for (i = 0; i < 7; i++) {

S3 = S3 + i ;

}

cout<<" SOM="<<S3<<endl;

}

Fonctions pourquoi ?

Un bloc de code (calcul + affichage des résultats)qui se répète ➔ encapsuler dans une fonction dont le type est void.

++- S5 … 3M. BENJELLOUN : 2019-2020 UMONS

void Fnct_Som (int N) {

int i, S=0;

for (i = 0; i <N; i++)

S = S + i ; //S+= i;

cout<<" SOM="<<S<< endl;

}

void main(){

Fnct_Som (5) ;

Fnct_Som (10) ;

Fnct_Som (7) ;

}

void main() {

int i, S1=0, S2=0, S3=0;

for (i = 0; i <5; i++) {

S1 = S1 + i ;

}

cout<<" SOM="<<S1<< endl;

for (i = 0; i <10; i++) {

S2 = S2 + i ;

}

cout<<" SOM="<<S2<< endl;

for (i = 0; i < 7; i++) {

S3 = S3 + i ;

}

cout<<" SOM="<<S3<<endl;

}

Fonctions pourquoi ?

++- S5 … 4M. BENJELLOUN : 2019-2020 UMONS

void main(){

Fnct_Som (5) ;

Fnct_Som (10) ;

Fnct_Som (7) ;

}

void Fnct_Som (int N) {

int i, S=0;

for (i = 0; i <N; i++)

S = S + i ;

cout<<" SOM="<<S<< endl;

}

5

Fonctions pourquoi ?

10

++- S5 … 5M. BENJELLOUN : 2019-2020 UMONS

Fonctions pourquoi ?

#include <iostream>

using namespace std;

void main() {

int i, S1=0, S2=0, S3=0;

for (i = 0; i <5; i++) {

S1 = S1 + i ;

}

for (i = 0; i <10; i++) {

S2 = S2 + i ;

}

for (i = 0; i < 7; i++) {

S3 = S3 + i ;

}

cout<<" SOM1="<<S1<<" SOM2="

<< S2 <<" SOM3="<<S3;

}

int i, S=0;

for (i = 0; i <N; i++)

S = S + i ; //S+=i;

Un bloc de code (calcul SANS affichage des résultats)qui se répète ➔ encapsuler dans une fonction dont le type est int pour transmettre le résultat de type int à la fonction main().

++- S5 … 6M. BENJELLOUN : 2019-2020 UMONS

Fonctions pourquoi ?

#include <iostream>

using namespace std;

void main() {

int i, S1=0, S2=0, S3=0;

for (i = 0; i <5; i++) {

S1 = S1 + i ;

}

for (i = 0; i <10; i++) {

S2 = S2 + i ;

}

for (i = 0; i < 7; i++) {

S3 = S3 + i ;

}

cout<<" SOM1="<<S1<<" SOM2="

<< S2 <<" SOM3="<<S3;

}

#include <iostream>

using namespace std;

int Fnct_Som(int N) {

int i, S=0;

for (i = 0; i <N; i++)

S = S + i ; //S+=i;

return S;

}

void main() {

int S1, S2, S3;

S1 = Fnct_Som(5) ;

S2 = Fnct_Som(10) ;

S3 = Fnct_Som(7) ;

cout<<" SOM1="<<S1<<" SOM2="

<< S2 <<" SOM3="<<S3;

}

++- S5 … 7M. BENJELLOUN : 2019-2020 UMONS

Fonctions pourquoi ?

#include <iostream>

using namespace std;

int Fnct_Som(int N) {

int i, S=0;

for (i = 0; i <N; i++)

S = S + i ; //S+=i;

return S;

}

void main() {

int S1, S2, S3;

S1 = Fnct_Som(5) ;

S2 = Fnct_Som(10) ;

S3 = Fnct_Som(7) ;

cout<<" SOM1="<<S1<<" SOM2="

<< S2 <<" SOM3="<<S3;

}

#include <iostream>

using namespace std;

void Fnct_Som (int N) {

int i, S=0;

for (i = 0; i <N; i++)

S = S + i ; //S+= i;

cout<<" SOM="<<S<< endl;

}

void main() {

Fnct_Som (5) ;

Fnct_Som (10) ;

Fnct_Som (7) ;

}

++- S5 … 8M. BENJELLOUN : 2019-2020 UMONS

void main(void){

int Val ;Val = addition();cout << "val = "<< Val<<endl;

}

int addition(){

float tmp;tmp = calcule(2.5,3) + calcule(5,7.2);

return (int)tmp;}

float calcule(float C, float D){

return ( (C + D ) / 2) ;}

float calcule(float, float);int addition();

float calcule(float A, float B){

return ( (A + B ) / 2) ;}

int addition(){ // Appel d’une fonction dans une fonction

float tmp;tmp = calcule(2.5,3) + calcule(5,7.2);

return (int)tmp;}

void main(void){

int Val ;Val = addition();cout << "val = "<< Val<<endl;

}

Déclaration et appel d’une fonction

Solution où il faut respecter l’ordre des définitions des fonctions

Solution où il n’est pas nécessaire de respecter l’ordre des définitions des fonctions

++- S5 … 9M. BENJELLOUN : 2019-2020 UMONS

#include <iostream>

using namespace std;

void Modifier(int v);

void main() {

int v = 5; // Variable locale à main()

Modifier(v); // Appel de la fonction Modifier

cout << "\n Main: v = " << v; /* Affiche la valeur de v après

passage dans la fonction Modifier (5)*/

}

void Modifier(int v) {

v = v *100; // Modifie la copie

cout << "Modifier: v = "<< v; /* Affiche la valeur de v dans la

fonction Modifier (500)*/

}

Les paramètres sont copiés. La fonction travaille donc sur une copie de v.

Déclaration et appel d’une fonction

Modifier: v = 500

Main: v = 5

++- S5 … 10M. BENJELLOUN : 2019-2020 UMONS

#include <iostream>

using namespace std;

void Addition(int v, float f);

void main() {

int v = 5;

float f= 4.5 ;

Addition(v, f); // Appel de la fonction

cout << "\n Main: v et f = " << v << "et " <<f; // (3eme)

}

void Addition(int v, float f) {

cout << " Addition: v + f = "<< v+f << endl; // (1er affichage)

f = f + v;

cout << " Addition: f = "<< f ; // (2eme)

}

Déclaration et appel d’une fonction

Addition : v+f = 9.5

Addition : f = 9.5

Main: v et f = 5 et 4.5

Les paramètres sont copiés. La fonction travaille donc sur une copie de v et f.

++- S5 … 11M. BENJELLOUN : 2019-2020 UMONS

Fonction Renvoyant une valeur au programme

#include <iostream>

using namespace std;

int return_Val(int X); // garder le même nom de la variable n’est pas obligatoire

void main( ) {int var = 5;int valeur;

valeur = return_Val (var);

cout <<"main: var = "<< var << endl; // (2)cout <<"main: valeur = "<< valeur; // (3)

}

int return_Val (int v){

v = 100;cout <<" return_Val : v = "<< v << endl; // (1)return (v+1);

}

Une fonction se termine et ‘rend la main’ à la fonction appelantelorsque son exécution rencontre l’instruction: return expression;ou return;

Déclaration et appel d’une fonction

return_Val : v = 100main: var = 5main: valeur = 101

++- S5 … 12M. BENJELLOUN : 2019-2020 UMONS

#include <iostream>using namespace std;

int return_Val (int Z);

void main( ) {int var = 5, valeur;

valeur = return_Val(var);

cout << "main: var = " << var << endl;cout << "main: valeur = "<< valeur;

}

int return_Val(int v) {if (v == 10) return (2*v);else return (3*v);

}

#include <iostream>using namespace std;

int return_Val (int K);

void main( ) {int var = 5, valeur;

valeur = return_Val(var);

cout << "main: var = " << var << endl;cout << "main: valeur = "<< valeur;

}

int return_Val(int v) {if (v == 10) return (2*v);else return (3*v);cout <<" return_Val v = "<< v<<endl;

}

Qu’affiche ce programme à l’écran ? Qu’affiche ce programme à l’écran ?

Déclaration et appel d’une fonction

++- S5 … 13M. BENJELLOUN : 2019-2020 UMONS

#include <iostream>using namespace std;

int return_Val (int Z);

void main( ) {int var = 5, valeur;

valeur = return_Val(var);

cout << "main: var = " << var << endl;cout << "main: valeur = "<< valeur;

}

int return_Val(int v) {if (v == 10) return (2*v);else return (3*v);

}

#include <iostream>using namespace std;

int return_Val (int K);

void main( ) {int var = 5, valeur;

valeur = return_Val(var);

cout << "main: var = " << var << endl;cout << "main: valeur = "<< valeur;

}

int return_Val(int v) {if (v == 10) return (2*v);else return (3*v);cout <<" return_Val v = "<< v<<endl;

}

Qu’affiche ce programme à l’écran ? Qu’affiche ce programme à l’écran ?

Déclaration et appel d’une fonction

main: var = 5

main: valeur = 15

main: var = 5

main: valeur = 15

++- S5 … 14M. BENJELLOUN : 2019-2020 UMONS

int return_Val(int v) {if (v == 10) return (2*v);else return (3*v);

}

int return_Val(int v) {if (v == 10) return (2*v);else return (3*v);cout <<" return_Val v = "<< v<<endl;

}

Déclaration et appel d’une fonction

==

L’instructioncout <<" return_Val v = "<< v<<endl;ne sera jamais exécutée car SI (v == 10) alors return (2*v) me sort de la fonctionSINON return (3*v) me sort de la fonction.

Fonction Renvoyant une valeur au programme

++- S5 … 15M. BENJELLOUN : 2019-2020 UMONS

Déclaration et appel d’une fonction Fonction Renvoyant une valeur au programme

int Fct_test() {

int i, j=1;char a;for (i = -10; i <= 10; i++){

…while(j!=0) // boucle infinie{

cin >> a; if(a=='Q')

return (1);} // while

}// for return (0);}

int Fct_test() {

int i, j=1;char a;for (i = -10; i <= 10; i++){

…while(j!=0) // boucle infinie {

cin >> a; if(a=='Q')

break;} // while

}// for return (0);}

//Sort de la fonction//Sort de la boucle while

++- S5 … 16M. BENJELLOUN : 2019-2020 UMONS

Référence#include …

void Modifier(int v);

void main(){

int v = 5;

Modifier(v);

cout << "\nmain: v = " << v;

}

void Modifier(int v){

v = v *100;

cout << "Modifier: v = "<< v;

}

Modifier: v = 500

main: v = 5

#include …

void Modifier(int *v);

void main(){

int v = 5;

Modifier(&v);

cout << "\nmain: v = " << v;

}

void Modifier(int *v){

*v = *v *100;

cout << "Modifier: *v = "<< *v;

}

#include …

void Modifier(int &v);

void main(){

int v = 5;

Modifier(v);

cout << "\nmain: v = " << v;

}

void Modifier(int &v){

v = v *100;

cout << "Modifier: v = "<< v;

}

par Valeur Pointeur

Appel par valeurs, pointeurs, références ??!!

Modifier: *v = 500

main: v = 500

Modifier: v = 500

main: v = 500

++- S5 … 17M. BENJELLOUN : 2019-2020 UMONS

#include …

void fonct (int a){

a=1 ;}

void main(void){int var = 5;fonct (var);cout << var << endl;

}

5

#include …

int fonct (int a){

a=1 ;return a;

}

void main(void){int var = 5;var = fonct (var);cout << var << endl;

}

1 1

#include …

void fonct (int *a){

*a=1 ;}

void main(void){int var = 5;fonct (&var);cout << var << endl;

}

#include …

void fonct (int &a){

a=1 ;}

void main(void){int var = 5;fonct (var);cout << var << endl;

}

1

Appel par valeurs, pointeurs, références ??!!

Pour sortir avec les modifications, utilisez « return » et ou pointeurs et ou références.

++- S5 … 18M. BENJELLOUN : 2019-2020 UMONS

#include <iostream> …void affiche (int a, int b) {

cout<<"\t i = " << a << " j = " << b << endl;}

void echange (int, int);void main () {

int i= 1, j=5;affiche (i, j);echange (i, j);affiche (i, j);

}void echange (int a, int b) {

int tmp;tmp = b;b = a;a = tmp;

}

void echange (int*, int*);void main () {

int i= 1, j=5;affiche (i, j);echange (&i, &j);affiche (i, j);

}void echange (int *a, int *b) {

int tmp;tmp = *b;*b = *a;*a = tmp;

}

void echange (int&, int&);void main () {

int i= 1, j=5;affiche (i, j);echange (i, j);affiche (i, j);

}void echange (int &a, int &b) {

int tmp;tmp = b;b = a;a = tmp;

}

i = 1 j = 5

i = 1 j = 5

i = 1 j = 5

i = 5 j = 1

i = 1 j = 5

i = 5 j = 1

par Valeur Pointeur Référence

Appel par valeurs, pointeurs, références ??!!

++- S5 … 19M. BENJELLOUN : 2019-2020 UMONS

#include <iostream> …

int return_Val(int , int);

void main(void){

int var1 = 5, var2=0;

int valeur;

valeur = return_Val(var1, var2);

cout << "var1 = " << var1

<< " var2 = "<<var2 << endl;

cout << "main: valeur = "<< valeur;

}

int return_Val(int v1, int v2) {

v1 *= 10;

v2 += 7;

return …;

}

Comment retourner v1 et v2?

#include <iostream> …

void return_Val(int &, int&);

void main(void){

int var1 = 5, var2=0;

int valeur;

valeur = return_Val(var1, var2);

cout << "var1 = " << var1

<< " var2 = "<<var2 << endl;

cout << "main: valeur = "<< valeur;

}

void return_Val(int &v1, int &v2) {

v1 *= 10;

v2 += 7;

return …;

}

var1 = 50 var2 = 7

Appel par ------------------, références

++- S5 … 20M. BENJELLOUN : 2019-2020 UMONS

#include <iostream> …

void somme(int , int , int &);

int modif(int , int &, int &);

void main(){

int a, b, c;

a = 2; b = 8;

somme(a, b, c);

cout <<"Somme de a="<<a<<" et b="<<b<<" : " << c << endl;

a = modif(a, b, c);

cout << "Modif : a="<<a<<" et b="<<b<<" : " << c << endl;

}

void somme(int x, int y, int &z){

z = x + y;

y = 29;

}

int modif(int x, int &y, int &z){

x *= 2; y= x+ y; z= 5;

return x;

}

Qu'affiche le code à l'écran ?

Modif : a= et b= :

Somme de a= et b= :

Appel par ------------------, références ??!!

++- S5 … 21M. BENJELLOUN : 2019-2020 UMONS

#include <iostream> …

void somme(int , int , int &);

int modif(int , int &, int &);

void main(){

int a, b, c;

a = 2; b = 8;

somme(a, b, c);

cout <<"Somme de a="<<a<<" et b="<<b<<" : " << c << endl;

a = modif(a, b, c);

cout << "Modif : a="<<a<<" et b="<<b<<" : " << c << endl;

}

void somme(int x, int y, int &z){

z = x + y;

y = 29;

}

int modif(int x, int &y, int &z){

x *= 2; y= x+ y; z= 5;

return x;

}

Qu'affiche le code à l'écran ?

Modif : a= et b= :

Somme de a= et b= :

Appel par ------------------, références ??!!

2 8 10

4 12 5

++- S5 … 22M. BENJELLOUN : 2019-2020 UMONS

Si x=1Si x=2Si x=3Si x=4Si x=5

…int Fonct1(int y){

y = 10; return (y);}void Fonct2(int y){

y = 11; exit(0);}void Fonct3(int y){

y = 12;}

void Fonct4(int &y){y = 13;

}

void main() {int x=0, y=0;cin >> x;

if (x== 1) y = Fonct1(y);if (x== 2) Fonct2(y);if (x== 3) return ( );if (x== 4) Fonct4(y);if (x== 5) Fonct3(y);

cout <<"ICI y=" << y ;}

Qu'affiche ce code à l'écran ?

++- S5 … 23M. BENJELLOUN : 2019-2020 UMONS

Si x=1Si x=2Si x=3Si x=4Si x=5

…int Fonct1(int y){

y = 10; return (y);}void Fonct2(int y){

y = 11; exit(0);}void Fonct3(int y){

y = 12;}

void Fonct4(int &y){y = 13;

}

void main() {int x=0, y=0;cin >> x;

if (x== 1) y = Fonct1(y);if (x== 2) Fonct2(y);if (x== 3) return ( );if (x== 4) Fonct4(y);if (x== 5) Fonct3(y);

cout <<"ICI y=" << y ;}

Si x=1 → y = Fonct1(y);→ y =10 → ICI y=10

Si x=2 → Fonct2(y);→ exit(0) →

Sort du programme → pas d’affichage

Si x=3 → return( ) → Sort de la fonction donc

de main() → pas d’affichage

Si x=4 → Fonct4(y) → Sort avec modif de y

→ ICI y=13

Si x=5 → Fonct3(y);→ y =12→ ICI y=0

++- S5 … 24M. BENJELLOUN : 2019-2020 UMONS

Règles de visibilité des variables

Le C++ est un langage structuré en blocs { } , les variables ne peuvent être utilisées que là où elles sont déclarées.

void main() {int i = 10;for ( i = 0; i <3 ; i++) {

cout << i << endl;}cout << i << endl;

}

void main() {int i = 10;for (int i = 0; i <3 ; i++) {

cout << i << endl;}cout << i << endl;

}

void main() {

for (int i = 0; i <3 ; i++) { cout << i << endl;

}cout << i << endl;

}

Qu'affiche ce code à l'écran ?

++- S5 … 25M. BENJELLOUN : 2019-2020 UMONS

Règles de visibilité des variables

Le C++ est un langage structuré en blocs { } , les variables ne peuvent être utilisées que là où elles sont déclarées.

void main() {int i = 10;for ( i = 0; i <3 ; i++) {

cout << i << endl;}cout << i << endl;

}

void main() {int i = 10;for (int i = 0; i <3 ; i++) {

cout << i << endl;}cout << i << endl;

}

void main() {

for (int i = 0; i <3 ; i++) { cout << i << endl;

}cout << i << endl;

}

0 1 2 3

0 1 2 10

erreur

++- S5 … 26M. BENJELLOUN : 2019-2020 UMONS

Règles de visibilité des variables

Le C++ est un langage structuré en blocs { } , les variables ne peuvent être utilisées que là où elles sont déclarées.

#include <iostream>using namespace std ;

int i = 0 ;

void optimist( ) { cout << "il fait beau " << i << " fois\n" ;

}void main() {

for (i=1 ; i<=5 ; i++)optimist() ;

}

Qu'affiche ce code à l'écran ?

++- S5 … 27M. BENJELLOUN : 2019-2020 UMONS

Règles de visibilité des variables

Le C++ est un langage structuré en blocs { } , les variables ne peuvent être utilisées que là où elles sont déclarées.

#include <iostream>using namespace std ;

int i = 0 ;

void optimist( ) { cout << "il fait beau " << i << " fois\n" ;

}void main() {

for (i=1 ; i<=5 ; i++)optimist() ;

}il fait beau 1 foisil fait beau 2 foisil fait beau 3 foisil fait beau 4 foisil fait beau 5 fois

++- S5 … 28M. BENJELLOUN : 2019-2020 UMONS

#include …

int globale=0; // 0

void fonc(int v) {double d, f; i++;globale --; // 9

}

void main(void) {int i = 5, j; float f = 2.8, g;d = 3.7;globale =10; // 10cout << " valeur de j= " << j ; // ???cout << "\nglobale = " << globale ; //10fonc (i);cout << "\nglobale = " << globale ; //9

}

(0)

(1)

(2)

(3)

(4)

(5)

#include …

int g;

void affichage(int un, int deux) {cout << un << " " << deux << " " << g << endl;

}

void fonct(int un, int deux){

affichage(un, deux);

un += 2; deux += 2; g += 2;

affichage(un, deux);

}

void main( ) {

int i = 5, j;

affichage(i, j);

j=10;

fonct(i, j);

affichage(i, j);

}

5 -858993460 0

5 10 0

7 12 2

5 10 2

??(-857…)

10

9

Règles de visibilité des variables

++- S5 … 29M. BENJELLOUN : 2019-2020 UMONS

#include <iostream>using namespace std ;

int k=0;

void Fct1(){k++;cout << "\nFct1: k = "<< k;

}

void Fct2() { // (1)k++;cout << "\nFct2: k = "<< k;

}

void main(){cout << "main: k = " << k; Fct1();Fct2();Fct1();Fct2();cout <<"\nmain: k = " << k;

}

Qu’affiche ce programme selon les instructions de la fonction Fct2 () en // (1), (2) et (3) ?

Règles de visibilité des variables

++- S5 … 30M. BENJELLOUN : 2019-2020 UMONS

#include <iostream>using namespace std ;

int k=0;

void Fct1(){k++;cout << "\nFct1: k = "<< k;

}

void Fct2() { // (1)k++;cout << "\nFct2: k = "<< k;

}

void main(){cout << "main: k = " << k; Fct1();Fct2();Fct1();Fct2();cout <<"\nmain: k = " << k;

}

Qu’affiche ce programme selon les instructions de la fonction Fct2 () en // (1), (2) et (3) ?

Règles de visibilité des variables

main: k = 0Fct1: k = 1Fct2: k = 2Fct1: k = 3Fct2: k = 4main: k = 4

(1)

++- S5 … 31M. BENJELLOUN : 2019-2020 UMONS

#include <iostream>using namespace std ;

int k=0;

void Fct1(){k++;cout << "\nFct1: k = "<< k;

}

void Fct2() { // (1)k++;cout << "\nFct2: k = "<< k;

}

void main(){cout << "main: k = " << k; Fct1();Fct2();Fct1();Fct2();cout <<"\nmain: k = " << k;

}

Qu’affiche ce programme selon les instructions de la fonction Fct2 () en // (1), (2) et (3) ?

Règles de visibilité des variables

main: k = 0Fct1: k = 1Fct2: k = 2Fct1: k = 3Fct2: k = 4main: k = 4

void Fct2() { // (2)int I=30; k++;cout << "\nFct2: k = "<< k; I++;cout<<"\n Fct2: I = "<< I;

}

(1) (2)

++- S5 … 32M. BENJELLOUN : 2019-2020 UMONS

#include <iostream>using namespace std ;

int k=0;

void Fct1(){k++;cout << "\nFct1: k = "<< k;

}

void Fct2() { // (1)k++;cout << "\nFct2: k = "<< k;

}

void main(){cout << "main: k = " << k; Fct1();Fct2();Fct1();Fct2();cout <<"\nmain: k = " << k;

}

Qu’affiche ce programme selon les instructions de la fonction Fct2 () en // (1), (2) et (3) ?

Règles de visibilité des variables

main: k = 0Fct1: k = 1Fct2: k = 2Fct1: k = 3Fct2: k = 4main: k = 4

void Fct2() { // (2)int I=30; k++;cout << "\nFct2: k = "<< k; I++;cout<<"\n Fct2: I = "<< I;

}

main: k = 0Fct1: k = 1Fct2: k = 2Fct2: I = 31

Fct1: k = 3Fct2: k = 4Fct2: I = 31

main: k = 4(1) (2)

++- S5 … 33M. BENJELLOUN : 2019-2020 UMONS

#include <iostream>using namespace std ;

int k=0;

void Fct1(){k++;cout << "\nFct1: k = "<< k;

}

void Fct2() { // (1)k++;cout << "\nFct2: k = "<< k;

}

void main(){cout << "main: k = " << k; Fct1();Fct2();Fct1();Fct2();cout <<"\nmain: k = " << k;

}

Qu’affiche ce programme selon les instructions de la fonction Fct2 () en // (1), (2) et (3) ?

void Fct2() { // (3)static int I=30; k++;cout << "\nFct2: k = "<< k; I++;cout<<"\n Fct2: I = "<< I;

}

Règles de visibilité des variables

main: k = 0Fct1: k = 1Fct2: k = 2Fct1: k = 3Fct2: k = 4main: k = 4

void Fct2() { // (2)int I=30; k++;cout << "\nFct2: k = "<< k; I++;cout<<"\n Fct2: I = "<< I;

}

main: k = 0Fct1: k = 1Fct2: k = 2Fct2: I = 31

Fct1: k = 3Fct2: k = 4Fct2: I = 31

main: k = 4(1) (2) (3)

++- S5 … 34M. BENJELLOUN : 2019-2020 UMONS

#include <iostream>using namespace std ;

int k=0;

void Fct1(){k++;cout << "\nFct1: k = "<< k;

}

void Fct2() { // (1)k++;cout << "\nFct2: k = "<< k;

}

void main(){cout << "main: k = " << k; Fct1();Fct2();Fct1();Fct2();cout <<"\nmain: k = " << k;

}

Qu’affiche ce programme selon les instructions de la fonction Fct2 () en // (1), (2) et (3) ?

void Fct2() { // (3)static int I=30; k++;cout << "\nFct2: k = "<< k; I++;cout<<"\n Fct2: I = "<< I;

}

Règles de visibilité des variables

main: k = 0Fct1: k = 1Fct2: k = 2Fct1: k = 3Fct2: k = 4main: k = 4

void Fct2() { // (2)int I=30; k++;cout << "\nFct2: k = "<< k; I++;cout<<"\n Fct2: I = "<< I;

}

main: k = 0Fct1: k = 1Fct2: k = 2Fct2: I = 31

Fct1: k = 3Fct2: k = 4Fct2: I = 31

main: k = 4

main: k = 0Fct1: k = 1Fct2: k = 2Fct2: I = 31

Fct1: k = 3Fct2: k = 4Fct2: I = 32

main: k = 4(1) (2) (3)

++- S5 … 35M. BENJELLOUN : 2019-2020 UMONS

Passer des tableaux aux fonctions

* Les tableaux peuvent être passés comme paramètres d'une fonction.

* Ils ne peuvent pas être retournés comme résultat d'une fonction.

* La longueur du tableau ne doit pas être définie à la déclaration de la fonction.

* Un tableau peut être modifié dans une fonction,.il est passé par référence (adresse) et non par valeur.

!

#include …

void Modif(int a[]) {a[0] = 5;a[1] = 6;

}

void main( ) {int p[2] = { 1, 2 };

cout << p[0] << " ; " << p[1];

Modif(p);

cout << p[0] << " ; " << p[1];}

1 ; 2

5 ; 6

#include …void Modif(int x[], int n);

void main( ) {int i;int p[6] = { 1, 2, 3, 5, 7, 11};Modif(p, 6);for (i=0; i<6; i++)

cout << p[i]<< endl;}

void Modif(int a[], int n) {int i; for(i = 0; i <n ; i++)

a[i] = 5;}

5

5

5

5

5

5

++- S5 … 36M. BENJELLOUN : 2019-2020 UMONS

.. .. ..

const int Nmax=20;

void saisie (int N, int T [ ]) {cout<<"Entrez les données du tableau "<<endl;for(int i=0 ; i <N ; i++) {

cout<<"entrez la valeur "<<i+1<<endl;cin>> T[i];

}

}

void affichage (int N, int T [ ]) {cout<< " Les valeurs sont : " <<endl;for(int i=0 ; i < N ; i++ )

cout<<" T["<<i+1<< " ] = "<< T[i]<<endl;

}

void main () {

int N= 5, T[Nmax];

saisie (N, T);affichage (N, T);

}

// Remplissage du tableau T[ ]

// Affichage des données du tableau T[ ]

// Programme principal

Passer des tableaux aux fonctions

++- S5 … 37M. BENJELLOUN : 2019-2020 UMONS

#include …

const int Nmax = 7;

int LaSomme(int A[], int N) {int S = 0;for (int i=0;i< N ;i++) {

cout <<"Entrez A["<<i<<"]=" ;cin >>A[i];S= S+ A[i];

}return S;

}

void main() {int A[Nmax], S=0, N=3;

S = LaSomme( A, N);cout << " Affichage :" << endl;Affichage(A, N);cout << "\n La somme est = "<< S ;

}

Qu’affiche ce programme à l’écran ?void Affichage( int A[], int N) {for (int i=0;i < N ;i++) {

cout << " A["<<i<<"]= " <<A[i] <<endl;

}}

Tableaux et fonctions

Entrez A[0] = 1Entrez A[1] = 2Entrez A[2] = 5

Affichage :A[0] = 1A[1] = 2A[2] = 5

La somme est = 8

++- S5 … 38M. BENJELLOUN : 2019-2020 UMONS

#include …

const int Nmax = 7;

int LaSomme( ) {int A[Nmax], N, S = 0;for (int i=0;i< N ;i++) {

cout <<"Entrez A["<<i<<"]=" ;cin >>A[i];S= S+ A[i];

}return S;

}

void main() {int A[Nmax], S=0, N=3;

S = LaSomme( );cout << " Affichage :" << endl;Affichage(A, N);cout << "\n La somme est = "<< S ;

}

Qu’affiche ce programme à l’écran ?void Affichage( int A[], int N) {for (int i=0;i < N ;i++) {

cout << " A["<<i<<"]= " <<A[i] <<endl;

}}

Tableaux et fonctions

++- S5 … 39M. BENJELLOUN : 2019-2020 UMONS

#include …

const int Nmax = 7;

int LaSomme(int A[] ) {int S = 0, N=5;for (int i=0;i< N ;i++) {

cout <<"Entrez A["<<i<<"]=" ;cin >>A[i];S= S+ A[i];

}return S;

}

void main() {int A[Nmax], S=0, N=3;

S = LaSomme( A);cout << " Affichage :" << endl;Affichage(A, N);cout << "\n La somme est = "<< S ;

}

Qu’affiche ce programme à l’écran ?void Affichage( int A[], int N) {for (int i=0;i < N ;i++) {

cout << " A["<<i<<"]= " <<A[i] <<endl;

}}

Tableaux et fonctions

++- S5 … 40M. BENJELLOUN : 2019-2020 UMONS

void Saisie_Mat( int tab[][4], int m, int n) {

for (int i = 0; i <m; i++)

for (int j = 0; j < n; j++) tab[i][j] = i;

}

Exemple

void Affiche_Mat( int tab[][4], int m, int n) {

for (int i = 0; i <m; i++) {

for (int j = 0; j < n; j++) cout << tab[i][j] << " ";

cout << endl;

}

}

#include <iostream>using namespace std;

void main() {

int T[5][5];

Saisie_Mat( T , 4, 4 );

Affiche_Mat( T , 4, 4 );

}

Matrice et fonctions

0 0 0 0

1 1 1 1

2 2 2 2

3 3 3 3

Résultat :