• Aucun résultat trouvé

6 Les fonctions

N/A
N/A
Protected

Academic year: 2022

Partager "6 Les fonctions"

Copied!
191
0
0

Texte intégral

(1)

6 Les fonctions

Peter Schlagheck Universit ´e de Li `ege

Ces notes ont pour seule vocation d’ ˆetre utilis ´ees par les ´etudiants dans le cadre de leur cursus au sein de l’Universit ´e de Li `ege. Aucun autre usage ni diffusion n’est autoris ´e, sous peine de constituer une violation de la Loi du 30 juin 1994

relative au droit d’auteur.

(2)

6 Les fonctions

6.1 Un exemple

6.2 La structure g ´en ´erale d’une fonction 6.3 Les routines

6.4 La r ´ecursion

6.5 Quelques fonctions standards

(3)

6.1 Un exemple

On a besoin d’effectuer une certaine action plusieurs fois dans des contextes diff ´erents:

include <iostream>

using namespace std;

main() { a ...

a hactioni ; a ...

a if ( ... ) a {

a a hactioni ; a a ...

a }

a hactioni ; a ...

}

o `u hactioni effectue p. ex.

hinstruction1i ; hinstruction2i ;

while ( hexpressioni ) {

a hinstruction3i ; a hinstruction4i ; }

hinstruction5i ;

...chaque fois avec d’autres variables

(4)

6.1 Un exemple d’une fonction

Programme pour calculer des nombres entiers k, l, m qui satisfont `a k n + l n = m n pour un exposant n > 0 donn ´e

−→ probl `eme de Fermat

Calcul de la puissance n d’un nombre entier:

int k, n;

cout << ''entrez les deux entiers k et n:'';

cin >> k >> n;

int kn = 1;

for ( int i = 1; i <= n; i++ ) kn *= k;

cout << k << ''ˆ'' << n << '' = '' << kn << endl;

(5)

6.1 Un exemple d’une fonction

Programme pour calculer des nombres entiers k, l, m qui satisfont `a k n + l n = m n pour un exposant n > 0 donn ´e

−→ probl `eme de Fermat

Calcul de la puissance n d’un nombre entier:

int k, n;

cout << ''entrez les deux entiers k et n:'';

cin >> k >> n;

int kn = 1;

for ( int i = 1; i <= n; i++ ) kn *= k;

cout << k << ''ˆ'' << n << '' = '' << kn << endl;

Ex ´ecution:

entrez deux entiers k et n: 3 4

3ˆ4 = 81

(6)

6.1 Un exemple d’une fonction

Programme pour calculer des nombres entiers k, l, m qui satisfont `a k n + l n = m n pour un exposant n > 0 donn ´e

int n, Nmax;

cout << ''exposant:'';

cin >> n;

cout << ''nombre maximal:'';

cin >> Nmax;

for ( int k = 1; k <= Nmax; k++ ) for ( int l = 1; l <= Nmax; l++ ) for ( int m = 1; m <= Nmax; m++ ) {

fo// calcul de kˆn foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

fo// calcul de lˆn foint ln = 1;

fofor ( int i = 1; i <= n; i++ ) fofoln *= l;

fo// calcul de mˆn foint mn = 1;

fofor ( int i = 1; i <= n; i++ ) fofomn *= m;

foif ( kn + ln == mn )

fofocout << k << ''ˆ'' << n << '' + '' fofocout<< l << ''ˆ'' << n << '' = '' fofocout<< m << ''ˆ'' << n << endl;

}

(7)

6.1 Un exemple d’une fonction

Programme pour calculer des nombres entiers k, l, m qui satisfont `a k n + l n = m n pour un exposant n > 0 donn ´e

int n, Nmax;

cout << ''exposant:'';

cin >> n;

cout << ''nombre maximal:'';

cin >> Nmax;

for ( int k = 1; k <= Nmax; k++ ) for ( int l = 1; l <= Nmax; l++ ) for ( int m = 1; m <= Nmax; m++ ) {

fo// calcul de kˆn foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

fo// calcul de lˆn foint ln = 1;

fofor ( int i = 1; i <= n; i++ ) fofoln *= l;

fo// calcul de mˆn foint mn = 1;

fofor ( int i = 1; i <= n; i++ ) fofomn *= m;

foif ( kn + ln == mn )

fofocout << k << ''ˆ'' << n << '' + '' fofocout<< l << ''ˆ'' << n << '' = '' fofocout<< m << ''ˆ'' << n << endl;

}

Ex ´ecution:

exposant: 2

nombre maximal: 10

3ˆ2 + 4ˆ2 = 5ˆ2

4ˆ2 + 3ˆ2 = 5ˆ2

6ˆ2 + 8ˆ2 = 10ˆ2

8ˆ2 + 6ˆ2 = 10ˆ2

(8)

6.1 Un exemple d’une fonction

Programme pour calculer des nombres entiers k, l, m qui satisfont `a k n + l n = m n pour un exposant n > 0 donn ´e

int n, Nmax;

cout << ''exposant:'';

cin >> n;

cout << ''nombre maximal:'';

cin >> Nmax;

for ( int k = 1; k <= Nmax; k++ ) for ( int l = 1; l <= Nmax; l++ ) for ( int m = 1; m <= Nmax; m++ ) {

fo// calcul de kˆn foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

fo// calcul de lˆn foint ln = 1;

fofor ( int i = 1; i <= n; i++ ) fofoln *= l;

fo// calcul de mˆn foint mn = 1;

fofor ( int i = 1; i <= n; i++ ) fofomn *= m;

foif ( kn + ln == mn )

fofocout << k << ''ˆ'' << n << '' + '' fofocout<< l << ''ˆ'' << n << '' = '' fofocout<< m << ''ˆ'' << n << endl;

}

Ex ´ecution:

exposant: 3

nombre maximal: 10

(9)

6.1 Un exemple d’une fonction

Programme pour calculer des nombres entiers k, l, m qui satisfont `a k n + l n = m n pour un exposant n > 0 donn ´e

int n, Nmax;

cout << ''exposant:'';

cin >> n;

cout << ''nombre maximal:'';

cin >> Nmax;

for ( int k = 1; k <= Nmax; k++ ) for ( int l = 1; l <= Nmax; l++ ) for ( int m = 1; m <= Nmax; m++ ) {

fo// calcul de kˆn foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

fo// calcul de lˆn foint ln = 1;

fofor ( int i = 1; i <= n; i++ ) fofoln *= l;

fo// calcul de mˆn foint mn = 1;

fofor ( int i = 1; i <= n; i++ ) fofomn *= m;

foif ( kn + ln == mn )

fofocout << k << ''ˆ'' << n << '' + '' fofocout<< l << ''ˆ'' << n << '' = '' fofocout<< m << ''ˆ'' << n << endl;

}

Ex ´ecution:

exposant: 3

nombre maximal: 100

(10)

6.1 Un exemple d’une fonction

Programme pour calculer des nombres entiers k, l, m qui satisfont `a k n + l n = m n pour un exposant n > 0 donn ´e

int n, Nmax;

cout << ''exposant:'';

cin >> n;

cout << ''nombre maximal:'';

cin >> Nmax;

for ( int k = 1; k <= Nmax; k++ ) for ( int l = 1; l <= Nmax; l++ ) for ( int m = 1; m <= Nmax; m++ ) {

fo// calcul de kˆn foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

fo// calcul de lˆn foint ln = 1;

fofor ( int i = 1; i <= n; i++ ) fofoln *= l;

fo// calcul de mˆn foint mn = 1;

fofor ( int i = 1; i <= n; i++ ) fofomn *= m;

foif ( kn + ln == mn )

fofocout << k << ''ˆ'' << n << '' + '' fofocout<< l << ''ˆ'' << n << '' = '' fofocout<< m << ''ˆ'' << n << endl;

}

Probl `eme:

on doit ´ecrire trois fois la m ˆeme it ´eration

Solution:

copier & coller dans l’ ´editeur. . .

ou la d ´efinition d’une fonction

(11)

6.1 Un exemple d’une fonction

Programme pour calculer des nombres entiers k, l, m qui satisfont `a k n + l n = m n pour un exposant n > 0 donn ´e

int n, Nmax;

cout << ''exposant:'';

cin >> n;

cout << ''nombre maximal:'';

cin >> Nmax;

for ( int k = 1; k <= Nmax; k++ ) for ( int l = 1; l <= Nmax; l++ ) for ( int m = 1; m <= Nmax; m++ ) foif ( puissance( k, n ) + foif (puissance( l, n ) ==

foif (puissance( m, n ) )

fofocout << k << ''ˆ'' << n << '' + '' fofocout<< l << ''ˆ'' << n << '' = '' fofocout<< m << ''ˆ'' << n << endl;

Probl `eme:

on doit ´ecrire trois fois la m ˆeme it ´eration

Solution:

copier & coller dans l’ ´editeur. . .

ou la d ´efinition d’une fonction

puissance( k, n )

(12)

6.1 Un exemple d’une fonction

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofo kn *= k;

foreturn kn;

}

(13)

6.1 Un exemple d’une fonction

#include <iostream>

using namespace std;

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} main() {

foint n, Nmax;

focout << ''exposant:'';

focin >> n;

focout << ''nombre maximal:'';

focin >> Nmax;

fofor ( int k = 1; k <= Nmax; k++ ) fofor ( int l = 1; l <= Nmax; l++ ) fofor ( int m = 1; m <= Nmax; m++ ) fofoif ( puissance( k, n ) + fofoif (puissance( l, n ) ==

fofoif (puissance( m, n ) )

fofofocout << k << ''ˆ'' << n << '' + '' fofofocout<< l << ''ˆ'' << n << '' = '' fofofocout<< m << ''ˆ'' << n << endl;

}

(14)

6.1 Un exemple d’une fonction

#include <iostream>

using namespace std;

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

foint s = puissance( n, k );

focout << r + s << endl;

}

Le flot du programme dans la m ´emoire:

(15)

6.1 Un exemple d’une fonction

#include <iostream>

using namespace std;

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

foint s = puissance( n, k );

focout << r + s << endl;

}

Le flot du programme dans la m ´emoire:

(16)

6.1 Un exemple d’une fonction

#include <iostream>

using namespace std;

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

foint s = puissance( n, k );

focout << r + s << endl;

}

Le flot du programme dans la m ´emoire:

(17)

6.1 Un exemple d’une fonction

#include <iostream>

using namespace std;

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

foint s = puissance( n, k );

focout << r + s << endl;

}

Le flot du programme dans la m ´emoire:

(18)

6.1 Un exemple d’une fonction

#include <iostream>

using namespace std;

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

foint s = puissance( n, k );

focout << r + s << endl;

}

Le flot du programme dans la m ´emoire:

(19)

6.1 Un exemple d’une fonction

#include <iostream>

using namespace std;

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

foint s = puissance( n, k );

focout << r + s << endl;

}

Le flot du programme dans la m ´emoire:

(20)

6.1 Un exemple d’une fonction

#include <iostream>

using namespace std;

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

foint s = puissance( n, k );

focout << r + s << endl;

}

Le flot du programme dans la m ´emoire:

(21)

6.1 Un exemple d’une fonction

#include <iostream>

using namespace std;

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

foint s = puissance( n, k );

focout << r + s << endl;

}

Le flot du programme dans la m ´emoire:

(22)

6.1 Un exemple d’une fonction

#include <iostream>

using namespace std;

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

foint s = puissance( n, k );

focout << r + s << endl;

}

Le flot du programme dans la m ´emoire:

(23)

6.1 Un exemple d’une fonction

#include <iostream>

using namespace std;

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

foint s = puissance( n, k );

focout << r + s << endl;

}

Le flot du programme dans la m ´emoire:

(24)

6.1 Un exemple d’une fonction

#include <iostream>

using namespace std;

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

foint s = puissance( n, k );

focout << r + s << endl;

}

Le flot du programme dans la m ´emoire:

(25)

6.1 Un exemple d’une fonction

#include <iostream>

using namespace std;

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

foint s = puissance( n, k );

focout << r + s << endl;

}

Le flot du programme dans la m ´emoire:

(26)

6.1 Un exemple d’une fonction

#include <iostream>

using namespace std;

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

foint s = puissance( n, k );

focout << r + s << endl;

}

Le flot du programme dans la m ´emoire:

(27)

6.1 Un exemple d’une fonction

#include <iostream>

using namespace std;

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

foint s = puissance( n, k );

focout << r + s << endl;

}

Le flot du programme dans la m ´emoire:

(28)

Ex ´ecution de la fonction puissance

#include <iostream>

using namespace std;

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

foint s = puissance( n, k );

focout << r + s << endl;

}

Ex ´ecution:

17

(29)

Ex ´ecution de la fonction puissance

#include <iostream>

using namespace std;

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} main() {

foint k = 2;

foint n = 3;

foint r = puissance( 2, 3 );

foint s = puissance( 3, 2 );

focout << r + s << endl;

}

Ex ´ecution:

17

(30)

Ex ´ecution de la fonction puissance

#include <iostream>

using namespace std;

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

foint s = puissance( n + k, k );

focout << r + s << endl;

}

Ex ´ecution:

33

(31)

Ex ´ecution de la fonction puissance

#include <iostream>

using namespace std;

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

fon = 6;

fok = 4;

foreturn kn;

} main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

foint s = puissance( n, k );

focout << r + s << endl;

focout << n << '' '' << k << endl;

}

Ex ´ecution:

17

3 2

(32)

Ex ´ecution de la fonction puissance

#include <iostream>

using namespace std;

int puissance( int k, int n ) {

foint kn = m;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} main() {

foint k = 2;

foint n = 3;

foint m = 1;

foint r = puissance( k, n );

foint s = puissance( n, k );

focout << r + s << endl;

}

−→ erreur: 'm' was not

−→ declared in this scope

(33)

Ex ´ecution de la fonction puissance

#include <iostream>

using namespace std;

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

fokn = 0;

foreturn kn;

} main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

foint s = puissance( n, k );

focout << r + s << endl;

}

Ex ´ecution:

17

(34)

Ex ´ecution de la fonction puissance

#include <iostream>

using namespace std;

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn 0;

foreturn kn;

} main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

foint s = puissance( n, k );

focout << r + s << endl;

}

Ex ´ecution:

0

(35)

6.2 La structure g ´en ´erale d’une fonction

La d ´efinition d’une fonction:

htypei hnomi ( htype1i hnom1i , htype2i hnom2i , ... ) {

fo hinstruction1i ; fo hinstruction2i ; fo . . .

fo return hexpression retouri ; }

−→ hexpression retouri doit ˆetre du type htypei

−→ (ou convertible dans ce type-l `a)

(36)

6.2 La structure g ´en ´erale d’une fonction

La d ´efinition d’une fonction:

htypei hnomi ( htype1i hnom1i , htype2i hnom2i , ... ) {

fo hinstruction1i ; fo hinstruction2i ; fo . . .

fo return hexpression retouri ; }

Appel de la fonction:

hnomi ( hexpression1i , hexpression2i , ... )

−→ rend hexpression retouri comme valeur

−→ hexpression1i doit ˆetre du type htype1i ,

−→ hexpression2i doit ˆetre du type htype2i , . . .

−→ (conversions implicites possibles)

(37)

6.2 La structure g ´en ´erale d’une fonction

L’appel de cette fonction corresponderait au code htypei hnomi ;

{

fo htype1i hnom1i = hexpression1i ; fo htype2i hnom2i = hexpression2i ; fo . . .

fo hinstruction1i ; fo hinstruction2i ; fo . . .

fo hnomi = hexpression retouri ; }

dans la partie du programme o `u cette fonction est appel ´ee

( `a l’exception qu’on ne pourrait pas utiliser d’autres variables du

programme principal dans ce bloc-l `a)

(38)

6.2 La structure g ´en ´erale d’une fonction

La d ´efinition d’une fonction:

htypei hnomi ( htype1i hnom1i , htype2i hnom2i , ... ) {

fo hinstruction1i ; fo hinstruction2i ; fo . . .

fo return hexpression retouri ; }

Les variables hnom1i , hnom2i , . . . sont implicitement d ´efinies au

d ´ebut du bloc de la fonction (d ´efinition locale)

(39)

6.2 La structure g ´en ´erale d’une fonction

La d ´efinition d’une fonction:

htypei hnomi ( htype1i hnom1i , htype2i hnom2i , ... ) {

fo hinstruction1i ; fo hinstruction2i ; fo . . .

fo return hexpression retouri ; }

Appel de la fonction:

hnomi ( hexpression1i , hexpression2i , ... )

−→ rend hexpression retouri comme valeur

−→ hexpression1i , hexpression2i , . . . sont donc

−→ donn ´ees `a la fonction comme des copies

(40)

6.2 La structure g ´en ´erale d’une fonction

La d ´efinition d’une fonction:

htypei hnomi ( htype1i hnom1i , htype2i hnom2i , ... ) {

fo hinstruction1i ; fo hinstruction2i ; fo . . .

fo return hexpression retouri ; }

l’instruction

foreturn hexpression retouri ;

termine l’ex ´ecution de la s ´equence d’instructions de la fonction et rend la valeur de l’expression hexpression retouri

−→ il peut y avoir plusieurs instructions return

−→ dans une fonction

(41)

6.2 La structure g ´en ´erale d’une fonction

La d ´efinition d’une fonction:

htypei hnomi ( htype1i hnom1i , htype2i hnom2i , ... ) {

fo hinstruction1i ; fo hinstruction2i ; fo . . .

fo return hexpression retouri ; fo . . .

fo return hexpression retour2i ; fo . . .

}

(42)

Ex ´ecution de la fonction puissance

#include <iostream>

using namespace std;

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} main() {

foint k = -2;

foint n = -3;

foint r = puissance( k, n );

foint s = puissance( n, k );

focout << r + s << endl;

}

Ex ´ecution:

2

(43)

Ex ´ecution de la fonction puissance

#include <iostream>

using namespace std;

int puissance( int k, int n ) {

foif ( n < 0 ) fo{

fofocout << ''exposant negatif!'' << endl;

foforeturn 0;

fo}

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} main() {

foint k = -2;

foint n = -3;

foint r = puissance( k, n );

foint s = puissance( n, k );

focout << r + s << endl;

}

Ex ´ecution:

exposant negatif!

exposant negatif!

0

(44)

Ex ´ecution de la fonction puissance

#include <iostream>

using namespace std;

int puissance( int k, int n ) {

foif ( n < 0 ) fo{

fofocerr << ''exposant negatif!'' << endl;

foforeturn 0;

fo}

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} main() {

foint k = -2;

foint n = -3;

foint r = puissance( k, n );

foint s = puissance( n, k );

focout << r + s << endl;

}

Ex ´ecution:

exposant negatif!

exposant negatif!

0

(45)

Vos libert ´es en tant que programmeurs en C/C++

→ dans une fonction, vous pouvez faire tout ce qui est aussi possible dans main()

−→ d ´efinir des variables,

−→ placer des branchement et des boucles,

−→ . . .

(46)

Vos libert ´es en tant que programmeurs en C/C++

→ dans une fonction, vous pouvez faire tout ce qui est aussi possible dans main()

→ vos fonctions peuvent rendre n’importe quel type

(−→ des entiers, des flottants, des caract `eres, . . . )

(47)

Vos libert ´es en tant que programmeurs en C/C++

→ dans une fonction, vous pouvez faire tout ce qui est aussi possible dans main()

→ vos fonctions peuvent rendre n’importe quel type

→ elles peuvent contenir autant d’arguments que vous voulez

−→ m ˆeme pas d’arguments de tout !

(48)

Vos libert ´es en tant que programmeurs en C/C++

→ dans une fonction, vous pouvez faire tout ce qui est aussi possible dans main()

→ vos fonctions peuvent rendre n’importe quel type

→ elles peuvent contenir autant d’arguments que vous voulez

int maxint() {

foint i = 1;

fowhile ( i > 0 ) fofoi++;

foi--;

foreturn i;

}

Appel:

cout << maxint() << endl;

2147483647

(49)

Vos libert ´es en tant que programmeurs en C/C++

→ dans une fonction, vous pouvez faire tout ce qui est aussi possible dans main()

→ vos fonctions peuvent rendre n’importe quel type

→ elles peuvent contenir autant d’arguments que vous voulez

→ vous pouvez choisir n’importe quels noms pour vos fonctions . . .

. . . tant qu’il n’y ait pas d’ambiguit ´e avec d’autres fonctions

(50)

L’identification des fonctions

Au niveau du compilateur, une fonction s’identifie non

seulement par son nom, mais aussi par sa liste d’arguments.

−→ le “nom interne” de la fonction

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofo kn *= k;

foreturn kn;

}

−→ au niveau du compilateur sera donc

−→ puissance( int, int )

−→ vous pouvez d ´efinir d’autres fonctions nomm ´ees

−→ puissance( ... )

−→ pourvu que leurs listes d’arguments soient diff ´erentes

(51)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

focout << r << endl;

}

−→ d ´efinition des fonctions

−→ puissance( int, int )

−→ et

−→ puissance( double, int )

(52)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(53)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(54)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(55)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(56)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(57)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(58)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(59)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(60)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

8

(61)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = 3;

foint r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

8

−→ appel de la fonction

−→ puissance( int, int )

(62)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

fodouble k = 2;

foint n = 3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(63)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

fodouble k = 2;

foint n = 3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(64)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

fodouble k = 2;

foint n = 3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(65)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

fodouble k = 2;

foint n = 3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(66)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

fodouble k = 2;

foint n = 3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(67)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

fodouble k = 2;

foint n = 3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(68)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

fodouble k = 2;

foint n = 3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(69)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

fodouble k = 2;

foint n = 3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(70)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

fodouble k = 2;

foint n = 3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(71)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

fodouble k = 2;

foint n = 3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

8

(72)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

fodouble k = 2;

foint n = 3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

8

−→ appel de la fonction

−→ puissance( double, int )

(73)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

fodouble k = 2;

foint n = -3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(74)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

fodouble k = 2;

foint n = -3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(75)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

fodouble k = 2;

foint n = -3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(76)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

fodouble k = 2;

foint n = -3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(77)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

fodouble k = 2;

foint n = -3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(78)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

fodouble k = 2;

foint n = -3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(79)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

fodouble k = 2;

foint n = -3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(80)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

fodouble k = 2;

foint n = -3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(81)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

fodouble k = 2;

foint n = -3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(82)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

fodouble k = 2;

foint n = -3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

0.125

(83)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

fodouble k = 2;

foint n = -3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

0.125

−→ appel de la fonction

−→ puissance( double, int )

(84)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = -3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(85)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = -3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(86)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = -3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(87)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = -3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(88)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = -3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(89)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = -3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(90)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = -3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

(91)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = -3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

1

(92)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = -3;

fodouble r = puissance( k, n );

focout << r << endl;

}

Ex ´ecution:

1

−→ appel de la fonction

−→ puissance( int, int )

−→ pas de conversion implicite

(93)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = -3;

fodouble r = puissance( (double) k, n );

focout << r << endl;

}

(94)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = -3;

fodouble r = puissance( (double) k, n );

focout << r << endl;

}

Ex ´ecution:

(95)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = -3;

fodouble r = puissance( (double) k, n );

focout << r << endl;

}

Ex ´ecution:

(96)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = -3;

fodouble r = puissance( (double) k, n );

focout << r << endl;

}

Ex ´ecution:

(97)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = -3;

fodouble r = puissance( (double) k, n );

focout << r << endl;

}

Ex ´ecution:

(98)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = -3;

fodouble r = puissance( (double) k, n );

focout << r << endl;

}

Ex ´ecution:

(99)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = -3;

fodouble r = puissance( (double) k, n );

focout << r << endl;

}

Ex ´ecution:

(100)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = -3;

fodouble r = puissance( (double) k, n );

focout << r << endl;

}

Ex ´ecution:

(101)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = -3;

fodouble r = puissance( (double) k, n );

focout << r << endl;

}

Ex ´ecution:

(102)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = -3;

fodouble r = puissance( (double) k, n );

focout << r << endl;

}

Ex ´ecution:

(103)

Deux fonctions avec le m ˆeme nom

#include <iostream>

using namespace std;

fo

int puissance( int k, int n ) {

foint kn = 1;

fofor ( int i = 1; i <= n; i++ ) fofokn *= k;

foreturn kn;

} fo

double puissance( double k, int n ) {

fodouble kn = 1;

foif ( n < 0 )

fofofor ( int i = -1; i >= n; i-- ) fofofokn /= k;

foelse

fofofor ( int i = 1; i <= n; i++ ) fofofokn *= k;

foreturn kn;

} fo main() {

foint k = 2;

foint n = -3;

fodouble r = puissance( (double) k, n );

focout << r << endl;

}

Ex ´ecution:

0.125

Références

Documents relatifs

vendredi : gainage + endurance Tu peux au choix faire ton gainage puis la séance d’endurance soit faire les deux séances à deux moments de la journée Échauffement articulaire

Pour les projets qui tombent sous l'application des règles des aides d’État (transformation de matières premières agricoles de l'annexe I en produits qui ne sont plus des

Elle v´ erifie par ailleurs ´ evidemment les conditions initiales : d’apr` es le cours, c’est l’unique solution au probl` eme de Cauchy ´ etudi´ e.. B.5 D’apr` es la question

 Outre les 2 CV, vous pouvez joindre tout autre document que vous jugez pertinent à l’évaluation de votre projet.  Merci d’envoyer votre projet en 1 seul

Ouverture en incrémentiel ou Création du fichier log_ua Ecriture dans le fichier log_ua la date début – date fin Pour chaque user agents. Mettre dans l’ordre suivant le nombre

[r]

[r]

Délais de réalisation : les projets déposés dans le cadre de cet appel à projets devront être intégralement réalisés (c’est-à-dire l’achèvement physique