Top Banner
CHAPTER 5 Programmer-defined functio n 程程程程程程程程 Instroduction In this chapter, we consider the basics of progra mmer-defined functions with value parameters. Our examination includes a discussion of invocation, parameters, and the local and global scopes. 本本本本 本本本本本本本本本本本本本本本本本 本本本本本本本本 本本本 本本本本本本本本本本本本 。、、。
128

CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Jan 04, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

CHAPTER 5 Programmer-defined function 程序员定义的函数

Instroduction

In this chapter, we consider the basics of programmer-defined functions

with value parameters. Our examination includes a discussion of invocati

on, parameters, and the local and global scopes.

本章讨论 使用值参的程序员定义函数的基本概念。还包括函数调用、参数、局部作用域和全局作用域。

Page 2: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

5.1 BASICS 基本概念

functionfunction

parameters

Return value

Input stream data

OutputStream data

Model for flow of information to and from a function using value parameters

Page 3: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

For example

float CircleArea ( float r )

{ const float Pi = 3.1415f ;

return Pi * r * r ;

}

5.1.1 Function definition syntax 函数定义语法

Page 4: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

For example

float CircleArea ( float r )

{ const float Pi = 3.1415f ;

return Pi * r * r ;

}

5.1.1 Function definition syntax 函数定义语法

Function headerInterface

Page 5: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

For example

float CircleArea ( float r )

{{ const float Pi = 3.1415f ;

return Pi * r * r ;

}}

5.1.1 Function definition syntax 函数定义语法

Function body

Page 6: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

For example

float CircleArea ( float r )

{ const float Pi = 3.1415f ;

return Pi * r * r ;

}

5.1.1 Function definition syntax 函数定义语法

TypeType FunctionName ()

{ // statements

returnreturn expressionexpression ;

}

voidvoid FunctionName ()

{ // statements

returnreturn ;

}

Page 7: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

The correspondence between the actual parameters in an invocation and

the formal parameters of a programmer-defined function is determined

by the relative positions of the parameters.

5.1.2 Invocation and flow of control 调用和控制流

Page 8: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

#include<iostream.h>

int add(int , int ) ;

void main()

{ int add(int, int ) ;

int a, b, c ;

cin >> a>> b;

c = add(a,b) ;

cout << “c = ” << c << endl ;

}

int add(int i, int j )

{ i + + ; j + + ;

return ( i + j ); }

a b c

int a, b, c ;

Invocation function with value parameters

Page 9: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Invocation function with value parameters

#include<iostream.h>

int add(int , int ) ;

main()

{ int add(int, int ) ;

int a, b, c ;

cin >> a>> b;

c = add(a,b) ;

cout << “c = ” << c << endl ;

}

int add(int i, int j )

{ i + + ; j + + ;

return ( i + j ); }

a b c2 4

cin >> a>> b;

Page 10: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Invocation function with value parameters

#include<iostream.h>

int add(int , int ) ;

main()

{ int add(int, int ) ;

int a, b, c ;

cin >> a>> b;

c = add(a,b) ;

cout << “c = ” << c << endl ;

}

int add(int i, int j )

{ i + + ; j + + ;

return ( i + j ); }

a b c2 4

c = add(a,b) ;

Page 11: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Invocation function with value parameters

#include<iostream.h>

int add(int , int ) ;

main()

{ int add(int, int ) ;

int a, b, c ;

cin >> a>> b;

c = add(a,b) ;

cout << “c = ” << c << endl ;

}

int add(int i, int j )

{ i + + ; j + + ;

return ( i + j ); }

a b c

i j add

2 4

2 4

int add(int i, int j )

Page 12: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Invocation function with value parameters

2 4i j add

#include<iostream.h>

int add(int , int ) ;

main()

{ int add(int, int ) ;

int a, b, c ;

cin >> a>> b;

c = add(a,b) ;

cout << “c = ” << c << endl ;

}

int add(int i, int j )

{ i + + ; j + + ;

return ( i + j ); }

a b c2 4

2 433 55

{ i + + ; j + + ;

Page 13: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Invocation function with value parameters

#include<iostream.h>

int add(int , int ) ;

main()

{ int add(int, int ) ;

int a, b, c ;

cin >> a>> b;

c = add(a,b) ;

cout << “c = ” << c << endl ;

}

int add(int i, int j )

{ i + + ; j + + ;

return ( i + j ); }

a b c

i j add

2 4

2 433 55 8

3 + 5

return ( i + j ); }

Page 14: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Invocation function with value parameters

#include<iostream.h>

int add(int , int ) ;

main()

{ int add(int, int ) ;

int a, b, c ;

cin >> a>> b;

c = add(a,b) ;

cout << “c = ” << c << endl ;

}

int add(int i, int j )

{ i + + ; j + + ;

return ( i + j ); }

a b c

i j add

2 4 8

2 433 55 8

c = add(a,b) ;

Page 15: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Invocation function with value parameters

#include<iostream.h>

int add(int , int ) ;

main()

{ int add(int, int ) ;

int a, b, c ;

cin >> a>> b;

c = add(a,b) ;

cout << “c = ” << c << endl ;

}

int add(int i, int j )

{ i + + ; j + + ;

return ( i + j ); }

a b c2 4 8

c = add(a,b) ;

Page 16: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Invocation function with value parameters

#include<iostream.h>

int add(int , int ) ;

main()

{ int add(int, int ) ;

int a, b, c ;

cin >> a>> b;

c = add(a,b) ;

cout << “c = ” << c << endl ;

}

int add(int i, int j )

{ i + + ; j + + ;

return ( i + j ); }

a b c2 4 8

Output :c = 8

cout << “c = ” << c << endl ;

Page 17: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Computing the size of a donut.

The size of a cylinder is r2h.

The size of a donut can be calculated

by taking the difference in the sizes of

two cylinders.

5.2 A TASTY PROBLEM 一个诱人的问题

Page 18: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

• The size of a cylinder

5.2 A TASTY PROBLEM 一个诱人的问题

// CylinderVolume(): compute the size of a cylinder with

// radius r and height h

float CylinderVolume(float r, float h)

{ const float Pi = 3.1415f;

return Pi * r * r * h;

}

Page 19: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

• The size of a cylinder

5.2 A TASTY PROBLEM 一个诱人的问题

// DonutSize(): compute the size of a donut with outer

// edge radius Outer from the donut center, inner edge

// radius Inner from the donut center, and thickness

// Width

float DonutSize(float Outer, float Inner, float Width)

{ float OuterSize = CylinderVolume(Outer, Width);

float HoleSize = CylinderVolume(Inner, Width);

return OuterSize - HoleSize;

}

float CylinderVolume(float r, float h) ;

• compute the size of a donut

Page 20: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

• The size of a cylinder

5.2 A TASTY PROBLEM 一个诱人的问题

// DonutSize(): compute the size of a donut with outer

// edge radius Outer from the donut center, inner edge

// radius Inner from the donut center, and thickness

// Width

float DonutSize(float Outer, float Inner, float Width)

{ float OuterSize = CylinderVolume(Outer, Width);

float HoleSize = CylinderVolume(Inner, Width);

return OuterSize - HoleSize;

}

float CylinderVolume(float r, float h) ;

• compute the size of a donut

return CylinderVolume(Outer, Width) - CylinderVolume(Inner, Width) ;return CylinderVolume(Outer, Width) - CylinderVolume(Inner, Width) ;

Page 21: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

• The size of a cylinder

5.2 A TASTY PROBLEM 一个诱人的问题

float DonutSize(float Outer, float Inner, float Width) ;

float CylinderVolume(float r, float h) ;

• compute the size of a donut

• packaging, Compute size of a user-specified donut

#include <iostream>

#include <string>

using namespace std;

// prototyping

float DonutSize(float Outer, float Inner, float Width);

float CylinderVolume(float Radius, float Width);

// main(): manage computation and display of user- specified donut size

int main() { … } // Program 6.2

Page 22: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Define a function to compute the factorial of a number:

5.3 SOME USEFUL FUNCTIONS

1nif1...)1n(n

0nif1!n

// Factorial() : determine n! for parameter nint function ( int n ){ int nfactorial = 1 ; while ( n > 1 ) { nfactorial *= n ; --n; } return nfactorial ;}

How checkthe value of n

whether is sensible ?

How checkthe value of n

whether is sensible ?

Page 23: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

// Program 6.4 : Has a scope prblem#include <iostream.h>

void Mystery ( int a, int b ) ; //prototype

int main()

{ int i = 10 , j = 20; // local object definition

Mystery ( i , j ) ; // call Mystery with local object i and j

cout << a << endl << b << endl ; // no define a and b in main()

return 0 ;

}

5.5 THE LOCAL SCOPE 局部作用域

void Mystery ( int aa , int bb )

{ cout << aa << endl << bb << endl ;

aa = 1; bb = 1 ;

cout << aa << endl << bb << endl ;

return ;

}

Page 24: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

// Program 6.4 : Has a scope prblem#include <iostream.h>

void Mystery ( int a, int b ) ;

int main()

{ int i = 10 , j = 20;

Mystery ( i , j ) ;

cout << a << endl << b << endl ;

return 0 ;

}

5.5 THE LOCAL SCOPE 局部作用域

void Mystery ( int aa , int bb )

{ cout << aa << endl << bb << endl ;

aa = 1; bb = 1 ;

cout << aa << endl << bb << endl ;

return ;

}

10 20i j

aa bb

Page 25: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

// Program 6.4 : Has a scope prblem#include <iostream.h>

void Mystery ( int a, int b ) ;

int main()

{ int i = 10 , j = 20;

Mystery ( i , j ) ;

cout << a << endl << b << endl ;

return 0 ;

}

5.5 THE LOCAL SCOPE 局部作用域

void Mystery ( int aa , int bb )

{ cout << aa << endl << bb << endl ;

aa = 1; bb = 1 ;

cout << aa << endl << bb << endl ;

return ;

}

10 20i j

aa bb10 20

Page 26: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

// Program 6.4 : Has a scope prblem#include <iostream.h>

void Mystery ( int a, int b ) ;

int main()

{ int i = 10 , j = 20;

Mystery ( i , j ) ;

cout << a << endl << b << endl ;

return 0 ;

}

5.5 THE LOCAL SCOPE 局部作用域

void Mystery ( int aa , int bb )

{ cout << aa << endl << bb << endl ;

aa = 1; bb = 1 ;

cout << aa << endl << bb << endl ;

return ;

}

10 20i j

aa bb10 20

Page 27: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

// Program 6.4 : Has a scope prblem#include <iostream.h>

void Mystery ( int a, int b ) ;

int main()

{ int i = 10 , j = 20;

Mystery ( i , j ) ;

cout << a << endl << b << endl ;

return 0 ;

}

5.5 THE LOCAL SCOPE 局部作用域

void Mystery ( int aa , int bb )

{ cout << aa << endl << bb << endl ;

aa = 1; bb = 1 ;

cout << aa << endl << bb << endl ;

return ;

}

10 20i j

a ? b ?a ? b ?a ? b ?a ? b ?

Page 28: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

A local object can be used only

• in the block and in the nested block in which it has been defined.

• in a statement or nested block that occurs after its definition.

5.5.1 Local scope rules 局部作用域规则

Block

Is a list of statements nested within curly braces .

{

statements

}

Page 29: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

• In different blocks, names are independent one another.

5.5.2 Name reuse with object 对象名的重用

// Example# include <iostream.h>main ( ){void f ( ) ; int a , b = 1 ; // local object within main() cout << “a = ” << a << “, b = ” << b << endl ; f ( ) ;}void f ( ){ int a = 2 ; b = 3 ; // local object within f() cout << “a = ” << a << “, b = ” << b << endl ; return ;}

Page 30: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

• In different blocks, names are independent one another.

5.5.2 Name reuse with object 对象名的重用

• In nested blocks, as soon as the nested block that reused the name is

complete, the declaration of the encompassing block is back in effect.

#include<iostream.h>

void main()

{ int a = 10, b = 20 ;

cout << a << '\t' << b << endl ;

{ char a = 'A' ; int b=30 ;{ char a = 'A' ; int b=30 ;

a++ ; b++ ;a++ ; b++ ;

cout<<a<<'\t'<<b<<endl;cout<<a<<'\t'<<b<<endl;

}}

a++; b++;

cout<<a<<'\t'<<b<<endl;

}

For example

10 20

B 31

11 21

Output

Page 31: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

• In different blocks, names are independent one another.

5.5.2 Name reuse with object 对象名的重用

• In nested blocks, as soon as the nested block that reused the name is

complete, the declaration of the encompassing block is back in effect.

#include<iostream.h>

void main()

{ int a = 10, b = 20 ;

cout << a << '\t' << b << endl ;

char a = 'A' ; int b=30 ;char a = 'A' ; int b=30 ; // illegal

a++; b++;

cout<<a<<'\t'<<b<<endl;

}

For example

• A name cannot be redefined in the same block.

Page 32: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

5.6 THE GLOBAL SCOPE

• An object defined in the global scope is called a global object.

• A global object may be referenced by simply using its name in any

desired block.

• Scope operator :: indicates that the definition being used is the

global one.

• Global object are always initialized.

Page 33: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

#include<iostream.h>

int a ; char c ;int a ; char c ; //global scope, initializedvoid main()void main(){ cout << "global_a0: " << { cout << "global_a0: " << a a << '\t' << "global_c0: " << << '\t' << "global_c0: " << c c << endl ;<< endl ; //using global objects

{ int a a ; char c c ; //withins statement block cout <<"local_a0: " << a a << '\t' << "local_c0: " << cc << endl ; aa = 10 ; cc = 'K' ; // using local objects cout << "local_a1: " << aa << '\t' << "local_c1: " << cc << endl ; ::a = 123 ; ::c = 'p' ;::a = 123 ; ::c = 'p' ; //using global objects cout << "global_a1: " << ::a::a << '\t' << "global_c1: " << ::c::c << endl ; } cout << "global_a2: " << cout << "global_a2: " << aa << '\t' << "global_c2: " << << '\t' << "global_c2: " << cc << endl ; << endl ;}}

For example1

Global_a0: 0 Global_c0:

Local_a0: -858993460 Local_c0: ?

Local_a1: 10 Local_c1: K

Global_a1: 123 Global_c1: p

Global_a2: 123 Global_c2: p

Output

Page 34: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

For example2#include<iostream.h>int a, b = 1 ;int a, b = 1 ; // can be using within global void main(){ void f1(); void f2(); cout << "a1 = " << a << ", b1 = "<< b << endl ; f1() ; f2() ;}int i , j = 1 ;int i , j = 1 ; // can be using within f1 and f2void f1(){ cout << "a2 = " << ++a << ", b2 = "<< ++b << endl ; cout << "i1 = " << i << ", j1 = " << j << endl; return ;}void f2(){ cout << "a3 = " << ++a << ", b3 = " << ++b << endl ; cout << "i2 = " << ++i << ", j2 = " << ++j << endl ; return ;}

a1 = 0, b1 = 1

a2 = 1, b2 = 2

i1 = 0, j1 = 1

a3 = 2, b3 =3

i2 = 1, j2 = 2

Output

Page 35: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

For example3

a = 0, b = 1

a = 2, b = 3

Output

#include <iostream.h>

int a, b = 1 ; // global objects

void main()

{ void f() ;

cout << "a = " << a << ", b = " << b << endl ;

f() ;

}

void f()

{ int a = 2; b = 3 ; // local objects

cout << "a = " << a << ", b = " << b << endl ;

return ;

}

Page 36: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

A reference parameter passes an alias ( 别名 ) to the actual parameter.

5.7 REFERENCE PARAMETERS 引用参数

The interface of a function is

FunctionType FunctionName ( ParameterList ) ;

The ParameterList of value parameter is

ParameterType ParameterName

The ParameterList of reference parameter is

ParameterType & ParameterName

Indicates a reference parameter

Page 37: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

// Swap two values#include<iostream.h>

void Swap ( int & , int & ) ;

void main()

{ int a = 3 , b = 8 ;

cout << "a=" << a << ", b=" << b << endl ;

Swap ( a , b ) ;

cout << "after swapping...\n" ;

cout << "a=" << a << ", b=" << b << endl ;

}

void Swap ( int & x& x , int & y& y )

{ int temp = xx ;

xx = yy ;

yy = temp ;

}

3

a

8

b

xx

yy

Page 38: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

// Swap two values#include<iostream.h>

void Swap ( int & , int & ) ;

void main()

{ int a = 3 , b = 8 ;

cout << "a=" << a << ", b=" << b << endl ;

Swap ( a , b ) ;

cout << "after swapping...\n" ;

cout << "a=" << a << ", b=" << b << endl ;

}

void Swap ( int & x& x , int & y& y )

{ int temp = xx ;

xx = yy ;

yy = temp ;

}

3

a

8

b

xx

yy

Page 39: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

// Swap two values#include<iostream.h>

void Swap ( int & , int & ) ;

void main()

{ int a = 3 , b = 8 ;

cout << "a=" << a << ", b=" << b << endl ;

Swap ( a , b ) ;

cout << "after swapping...\n" ;

cout << "a=" << a << ", b=" << b << endl ;

}

void Swap ( int & x& x , int & y& y )

{ int temp = xx ;

xx = yy ;

yy = temp ;

}

8

a

3

b

xx

yy

a = 3, b = 8

after swaooing …

a = 8, b = 3

Output

Page 40: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

// Swap two values#include<iostream.h>

void Swap ( int & , int & ) ;

void main()

{ int a = 3 , b = 8 ;

cout << "a=" << a << ", b=" << b << endl ;

Swap ( a , b ) ;

cout << "after swapping...\n" ;

cout << "a=" << a << ", b=" << b << endl ;

}

void Swap ( int & x& x , int & y& y )

{ int temp = xx ;

xx = yy ;

yy = temp ;

}

Note

The actual parameters

that be connect with

reference parameters

must be names.

Follows are error:

Swap(a+b, b );Swap(a+b, b );

Swap(a, 23 ); Swap(a, 23 );

Page 41: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

// compare#include<iostream.h>

void Swap ( int & , int & ) ;

void main()

{ int a = 3 , b = 8 ;

cout << "a=" << a << ", b=" << b << endl ;

Swap ( a , b ) ;

cout << "after swapping...\n" ;

cout << "a=" << a << ", b=" << b << endl ;

}

3

a

8

b

void Swap ( int xx , int yy )

{ int temp = xx ;

xx = yy ;

yy = temp ;

}

xx

y

Page 42: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

// compare#include<iostream.h>

void Swap ( int & , int & ) ;

void main()

{ int a = 3 , b = 8 ;

cout << "a=" << a << ", b=" << b << endl ;

Swap ( a , b ) ;

cout << "after swapping...\n" ;

cout << "a=" << a << ", b=" << b << endl ;

}

3

a

8

b

void Swap ( int xx , int yy )

{ int temp = xx ;

xx = yy ;

yy = temp ;

}

xx

y

Page 43: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

// compare#include<iostream.h>

void Swap ( int & , int & ) ;

void main()

{ int a = 3 , b = 8 ;

cout << "a=" << a << ", b=" << b << endl ;

Swap ( a , b ) ;

cout << "after swapping...\n" ;

cout << "a=" << a << ", b=" << b << endl ;

}

3

a

8

b

void Swap ( int xx , int yy )

{ int temp = xx ;

xx = yy ;

yy = temp ;

}

3

xx

8

y

Page 44: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

// compare#include<iostream.h>

void Swap ( int & , int & ) ;

void main()

{ int a = 3 , b = 8 ;

cout << "a=" << a << ", b=" << b << endl ;

Swap ( a , b ) ;

cout << "after swapping...\n" ;

cout << "a=" << a << ", b=" << b << endl ;

}

3

a

8

b

void Swap ( int xx , int yy )

{ int temp = xx ;

xx = yy ;

yy = temp ;

}

8

xx

3

y

Page 45: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

// compare#include<iostream.h>

void Swap ( int & , int & ) ;

void main()

{ int a = 3 , b = 8 ;

cout << "a=" << a << ", b=" << b << endl ;

Swap ( a , b ) ;

cout << "after swapping...\n" ;

cout << "a=" << a << ", b=" << b << endl ;

}

3

a

8

b

void Swap ( int xx , int yy )

{ int temp = xx ;

xx = yy ;

yy = temp ;

}

a = 3, b = 8

after swaooing …

a = 3, b = 8

Output

Page 46: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

#include <iostream>

#include <string>

using namespace std;

// Swap(): swap two values

void Swap(int &x, int &y)

{ int tmp = x;

x = y; y = tmp;

return;

}

// Sort3(): sort three numbers into non-descending order

void Sort3(int &a, int &b, int &c)

{ if (a > b) Swap(a, b);

if (a > c) Swap(a, c);

if (b > c) Swap(b, c);

return;

}

// Program 6.7: Input three numbers and output them in sorted order

Page 47: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

// read three numbers and output them in sorted order

int main()

{ cout << "Please enter three integers: " << flush;

int Input1; int Input2; int Input3;

cin >> Input1 >> Input2 >> Input3;

int Output1 = Input1;

int Output2 = Input2;

int Output3 = Input3;

// Sort the three numbers

Sort3(Output1, Output2, Output3);

// Output the sorted numbers

cout << Input1 << " " << Input2 << " " << Input3<< " in sorted order is "

<< Output1 << " " << Output2 << " " << Output3<< endl;

return 0;

}

// Program 6.7: Input three numbers and output them in sorted order

Page 48: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

• When we extract data from a stream or insert data into a

stream, we are changing the stream. This change must be

reflected in the stream object.

• Thus whenever we need to pass a stream to a function, we

must pass the stream by reference.

5.8 PASSING OBJECTS BY REFERENCE

按引用传递对象

Page 49: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

For example

void OutputValue ( ostream & out , int Value )

{ out << " Value is " << Value << endl ; }

call

OutputValue ( cout , 256 ) ;

OutputValue ( cerr , 25+6 ) ;

OutputValue ( clog , 25/6 ) ;

ofstream outfile ( "MyOutfile" ) ;

OutputValue ( outfile , 25/6 ) ;

Page 50: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

For example

void OutputValue ( ostreamostream & out , int Value )

{ out << " Value is " << Value << endl ; }

call

OutputValue ( cout , 256 ) ;

OutputValue ( cerr , 25+6 ) ;

OutputValue ( clog , 25/6 ) ;

ofstreamofstream outfile ( "MyOutfile" ) ;

OutputValue ( outfile , 25/6 ) ;

Note

The type of actual parameter

is ofstream, but the type of f

ormal parameter is ostream.

They are different.

ofstream class is a derived c

lass of ostream.

Note

The type of actual parameter

is ofstream, but the type of f

ormal parameter is ostream.

They are different.

ofstream class is a derived c

lass of ostream.

Page 51: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

For example

// Reads three values from stream

bool ReadValues ( istream & in , int &v1 , int &v2 , int &v3)

{ if ( in == cin )

cout << “Please enter three numbers" ;

if ( in >> v1 >> v2 >> v3 )

return true ;

else

return false ;

}

Page 52: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

// Program 6.8: Sorting numbers read from a file#include <iostream>#include <fstream>#include <string>using namespace std;// ReadValues(): read three values, if from cin, then prompt userbool ReadValues(istream &in, int &v1, int &v2, int &v3){ if (in == cin) cout << "Please enter three numbers" << flush; if (in >> v1 >> v2 >> v3) return true; else return false;}// Swap(): swap two valuesvoid Swap(int &x, int &y) { int tmp = x; x = y; y = tmp; return; }// Sort3(): sort three numbers into non-descending ordervoid Sort3(int &a, int &b, int &c) { if (a > b) Swap(a, b); if (a > c) Swap(a, c); if (b > c) Swap(b, c); return;}

Page 53: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

int main()

{ // Open the input file

ifstream fin("mydata.nbr");

if (!fin)

{ cerr << "Could not open mydata.nbr" << endl; return 1; }

int Input1, Input2, Input3;

if (!ReadValues(fin, Input1, Input2, Input3))

{ cerr << "Could not read three values" << endl; return 1; }

int Output1 = Input1;

int Output2 = Input2;

int Output3 = Input3;

// Sort the three numbers

Sort3(Output1, Output2, Output3);

// Output the sorted numbers

cout << Input1 << " " << Input2 << " " << Input3 << " in sorted order is "

<< Output1 << " " << Output2 << " " << Output3<< endl;

return 0;

}

Page 54: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

A user type a five-digit number. The first three digits are added together.

The remainder of this sum when divided by the fourth digit should be

equal to the fifth digit.

Example:

12330 // OK

23531 // OK

12345 // Error

5.9 VALIDATING TELEPHONE ACCESS CODE

验证电话访问密码

Page 55: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

// Program 6.9: Validate a telephone access code#include <iostream>

#include <string>

#include <ctype.h>

using namespace std;

// Prototypes for utility functions that main will use

bool Get(istream &in, int &d1, int &d2, int &d3, int &d4, int &d5);

bool Valid(int d1, int d2, int d3, int d4, int d5);

int main()

{ int d1, d2, d3, d4, d5;

if (Get(cin, d1, d2, d3, d4, d5) && Valid(d1, d2, d3, d4, d5)) // Call functions

{ cout << "OK!\n" ; return 0 ; }

else

{ cout << "Error!" ; return 1 ; }

}

Page 56: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

// Inputs are five characters. They are turned into integral reference parameters

bool Get(istream &sin, int &d1, int &d2, int &d3,int &d4, int &d5)

{ char c1; char c2; char c3; char c4; char c5;

if (sin >> c1 >> c2 >> c3 >> c4 >> c5)

{ if (isdigitisdigit(c1) && isdigit(c2) && isdigit(c3)&& isdigit(c4) && isdigit(c5))

{ d1 = c1 - '0'; d2 = c2 - '0'; d3 = c3 - '0'; d4 = c4 - '0'; d5 = c5 - '0';

return true; }

else return false;

}

else return false;

}

// Examine the digit inputs and determine whether they represent a valid access code

bool Valid(int d1, int d2, int d3, int d4, int d5)

{ if (d4 == 0) return false;

else return ((d1 + d2 + d3) % d4) == d5;

}

Library function isdigit()isdigit()

• Definition in the ctype.h

• If its parameter is a decimal d

igit, it returns true; otherwise r

eturns false.

Library function isdigit()isdigit()

• Definition in the ctype.h

• If its parameter is a decimal d

igit, it returns true; otherwise r

eturns false.

Page 57: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

// Inputs are five characters. They are turned into integral reference parameters

bool Get(istream &sin, int &d1, int &d2, int &d3,int &d4, int &d5)

{ char c1; char c2; char c3; char c4; char c5;

if (sin >> c1 >> c2 >> c3 >> c4 >> c5)

{ if (isdigitisdigit(c1) && isdigit(c2) && isdigit(c3)&& isdigit(c4) && isdigit(c5))

{ d1 = c1 - '0'; d2 = c2 - '0'; d3 = c3 - '0'; d4 = c4 - '0'; d5 = c5 - '0';

return true; }

else return false;

}

else return false;

}

// Examine the digit inputs and determine whether they represent a valid access code

bool Valid(int d1, int d2, int d3, int d4, int d5)

{ if (d4 == 0) return false;

else return ((d1 + d2 + d3) % d4) == d5;

}

Page 58: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

• An object has the const modifier, it is “read only”.

• If the const modifier be used a parameter, the called function does not

modify the actual object.

5.10 CONSTANT PARAMETERS 常量参数

void Example ( const int a , int b , int c )

{ b = a + 3 ; // legal assignment

a = c + 5 ;a = c + 5 ; // illegal assignment

return ;

}

Page 59: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

• An object has the const modifier, it is “read only”.

• If the const modifier be used a parameter, the called function does not

modify the actual object.

5.10 CONSTANT PARAMETERS 常量参数

Preserve the object of actual parameter :

• Value parameters

a copy of the object is made and the copy is passed to the called function.

• Constant parameters

pass the object as a constant reference parameter. It is efficiency and

safety.

Page 60: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

// Program 6.10 :// Reformat a name from LastName, FirstName to FirstName <space> LastName

#include<iostream>

#include<string>

using namespace std ;

void ParseName (string &FirstName, string & LastName, const string & FullNamconst string & FullNam

ee)

{ int i=FullName.find(","); // i is order

LastName=FullName.substr(0,i); // i is length

FirstName=FullName.substr(i+2, FullName.size());

return ;

}

int main()

{ string Name="Stroustrup, Bjarne";

string FirstName , LastName;

ParseName(FirstName, LastName, NameName);

Name=FirstName+" "+LastName;

cout<<Name<<endl;

return 0;

}

Page 61: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

// Complier limitations on enforcing the const modifier

#include<iostream.h>

void foo (int &p1, int p2)

{ p1=p2+p1+10;

return;

}

void example( /*const*/ int & CValue)

{ foo(CValue,3);

return;

}

void main()

{ int Con=100;

example(Con);

cout<<Con<<endl;

}

A const object can not

be passed by reference to

another function.

Page 62: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

• Using default parameters,we can write a function that has a

default behavior.

• The default parameters must appear after any mandatory

parameters.

5.11 DEFAULT PARAMETERS 默认 参数

Page 63: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

// using a default parameter to control output

void OutputValues ( ostream & out , int Value1 , int Value2 ,

bool DoubleSpace = false )

{ out << “Value 1 is " <<Value1 << endl ;

if ( DoubleSpace ) out << endl ;

out << “Value 2 is " <<Value2 << endl ;

if ( DoubleSpace ) out << endl ;

}

5.11 DEFAULT PARAMETERS 默认 参数

Page 64: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

// Other example# include <iostream.h>

double power ( double real, int n = 2 ) ;

void main ( )

{ double r = 3.0 ;

cout << power ( r ) << endl ;

cout << power ( r, 3 ) << endl ;

}

double power ( double real , int n )

{ if ( n == 0 )

return 1.0 ;

double result = real ;

for ( int i = 2 ; i <= n ; i ++ )

result *= real ;

return result;

}

Page 65: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Declare default parameters:

int f ( ) ;

……

void delay ( int k , int time = f ( ) ) ; OKOK !!OKOK !!

Page 66: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Declare default parameters:

int f ( ) ;

……

void delay ( int k , int time = f ( ) ) ;

void ferror1 ( int x , int y = 1int y = 1 , int z ) ; ErrorError !!ErrorError !!

Page 67: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Declare default parameters:

int f ( ) ;

……

void delay ( int k , int time = f ( ) ) ;

void ferror1 ( int x , int y = 1int y = 1 , int z ) ;

void ferror2 ( int x , int y = 0 ) ;

void ferror2 ( int x ) ;

Call : ferror2 ( 3 ) ;ferror2 ( 3 ) ; // Who can be call ?

ErrorError !!ErrorError !!

Page 68: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

The C++ compiler convert actual parameter expression to the type of

formal parameters .

5.12 CASTING OF FUNCTION PARAMETERS

函数参数的类型转换

For example

void fun ( int a , double x ) ;

fun ( 155.8/3 , 45000 ) ;

Page 69: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Function overloading

Create several functions of the same that behave differently.

• A identifier can be name for several functions.

• Compiler can confirm a function that is called in accordance with

parameters.

• Can not confirm overloading functions if the type of that return are

different only.

5.13 FUNCTION OVERLOADING 函数重载

Page 70: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

// The number of parameters are parity, but type are different

# include <iostream.h>

int abs ( int aint a ) ;

double abs ( double fdouble f ) ;

void main ( )

{ cout << abs ( -5 ) << endl ;

cout << abs ( -7.8 ) << endl ;

}

int abs ( int a )

{ return a < 0 ? -a : a ; }

double abs ( double f )

{ return f < 0 ? -f : f ; }

? :? : triary ( 三元 ) operator

operand1 ?? operand2 :: operand3

If operand1 is true, the value of expre

ssion is operand2, otherwise that is o

perand3.

Example

int a=1, b=2, c ;

c = a ? a+b : a-b ;

// if (a) c=a+b; else c=a-b;

? :? : triary ( 三元 ) operator

operand1 ?? operand2 :: operand3

If operand1 is true, the value of expre

ssion is operand2, otherwise that is o

perand3.

Example

int a=1, b=2, c ;

c = a ? a+b : a-b ;

// if (a) c=a+b; else c=a-b;

Page 71: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

// The number of parameters different# include <iostream.h>

int max ( int a , int b ) ;int max ( int a , int b ) ;

int max ( int a , int b, int c ) ;int max ( int a , int b, int c ) ;

void main ( )

{ cout << max ( 5, 3 ) << endl ;

cout << max (4, 8, 2 ) << endl ;

}

int max ( int a , int b )

{ return a > b ? a : b ; }

int max ( int a , int b, int c )

{ int t ;

t = max ( a , b )max ( a , b ) ;

return max ( t , c )max ( t , c ) ;

}

Page 72: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

// Rewrite program : Swap two values#include<iostream.h>void Swap ( int & , int & ) ; void main(){ int a = 3 , b = 8 ; Swap ( a , b ) ; cout << "after swapping...\n" << "a=" << a << ", b=" << b << endl ; float x = 0.618 , y = 2.71828 ; Swap ( x , y ) ; cout << "after swapping...\n" << “x=" << x << ", y=" << y << endl ;}void Swap ( int & x , int & y ) { int temp = x ; x = y ; y = temp ;}void Swap ( float & x , float & y ) { float temp = x ; x = y ; y = temp ;}

void ferror2 ( int x , int y = 0 ) ;

void ferror2 ( int x ) ;

Call : ferror2 ( 3 ) ;ferror2 ( 3 ) ; // Who can be call ?

void ferror2 ( int x , int y = 0 ) ;

void ferror2 ( int x ) ;

Call : ferror2 ( 3 ) ;ferror2 ( 3 ) ; // Who can be call ?

Page 73: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

5.14 RECURSIVE FUNCTION 递归函数

Recursive definition

A object can be defined by itself or itself partly

Example1:

Natural number

1 is a natural number, the next natural numbers are natural number

Example2:

The definition of factorial

1nif)!1n(n

0nif1!n)n(Factorial

Page 74: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

• Recursion is the the ability of a function to call itself.

• The recursive function can be executed, because the local objects and

value parameters push into stack (压入堆栈) when the function is

recursive called. They are pop out (弹出) in opposite order when

unwinding.

5.14 RECURSIVE FUNCTION 递归函数

Recursive function

• Recursive model

• The condition of executing the recursion

• The condition of stopping the recursion

Page 75: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

int Factorial ( int n ) {

if ( n = = 0 )

return 1 ;

else

return n * Factorial ( n - 1 ) ;

}

1nif)!1n(n

0nif1!n)n(Factorial

Page 76: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

1nif)!1n(n

0nif1!n)n(Factorial

int Factorial ( int n ) {

if ( n = = 0 )

return 1 ;

else

return n * Factorial ( n - 1 ) ;

}

Recursive model

Page 77: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

1nif)!1n(n

0nif1!n)n(Factorial

int Factorial ( int n ) {

if ( n = = 0 )

return 1 ;

else

return n * Factorial ( n - 1 ) ;

}

condition of stopping Recursive

Page 78: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

1nif)!1n(n

0nif1!n)n(Factorial

int Factorial ( int n ) {

if ( n = = 0 )

return 1 ;

else

return n * Factorial ( n - 1 ) ;

}

Modify the condition Recursive

Page 79: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

int Factorial ( int n ) {

if ( n = = 0 ) return 1 ;

else return n * Factorial ( n - 1 ) ;

}

k 3

F (3)

Output 3! = 6Output 3! = 6

n 3

F (3) 3* F (2)3* F (2)

nn 22

F(2)F(2) 2*2* F(1)F(1)

n 1

F (0) 1

Compute Factorial (3) = 3!

F(1)F(1)

n 0

1*1* F(0)F(0)

Page 80: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

int Factorial ( int n ) {

if ( n = = 0 ) return 1 ;

else return n * Factorial ( n - 1 ) ;

}

k 3

F (3)

Output 3! = 6Output 3! = 6

n 3

F (3) 3* F (2)3* F (2)

nn 22

F(2)F(2) 2*2* F(1)F(1)

n 1

F (0) 1

Compute Factorial (3) = 3!

F(1)F(1)

n 0

1*1* F(0)F(0)1

Page 81: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

int Factorial ( int n ) {

if ( n = = 0 ) return 1 ;

else return n * Factorial ( n - 1 ) ;

}

k 3

F (3)

Output 3! = 6Output 3! = 6

n 3

F (3) 3* F (2)3* F (2)

nn 22

F(2)F(2) 2*2* F(1)F(1)

n 1

Compute Factorial (3) = 3!

F(1)F(1)

Page 82: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

int Factorial ( int n ) {

if ( n = = 0 ) return 1 ;

else return n * Factorial ( n - 1 ) ;

}

k 3

F (3)

Output 3! = 6Output 3! = 6

n 3

F (3) 3* F (2)3* F (2)

nn 22

F(2)F(2) 2*2* F(1)F(1)

n 1

Compute Factorial (3) = 3!

F(1)F(1)

F (1) = 1F (1) = 1

1

Page 83: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

int Factorial ( int n ) {

if ( n = = 0 ) return 1 ;

else return n * Factorial ( n - 1 ) ;

}

k 3

F (3)

Output 3! = 6Output 3! = 6

n 3

F (3) 3* F (2)3* F (2)

nn 22

F(2)F(2)

Compute Factorial (3) = 3!

F(2) = 2F(2) = 2

2

Page 84: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

int Factorial ( int n ) {

if ( n = = 0 ) return 1 ;

else return n * Factorial ( n - 1 ) ;

}

k 3

F (3)

Output 3! = 6Output 3! = 6

n 3

F (3)

Compute Factorial (3) = 3!

Page 85: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

int Factorial ( int n ) {

if ( n = = 0 ) return 1 ;

else return n * Factorial ( n - 1 ) ;

}

k 3

F (3)

Output 3! = 6Output 3! = 6

n 3

F (3)

Compute Factorial (3) = 3!

F(3) = 6F(3) = 6

Page 86: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

int Factorial ( int n ) {

if ( n = = 0 ) return 1 ;

else return n * Factorial ( n - 1 ) ;

}

k 3

F (3)

Output 3! = 6Output 3! = 6

Compute Factorial (3) = 3!

Page 87: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

int Factorial ( int n ) {

if ( n = = 0 ) return 1 ;

else return n * Factorial ( n - 1 ) ;

}

k 3

F (3)

Output 3! = 6Output 3! = 6

Compute Factorial (3) = 3!

3! = 6

Page 88: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

int Factorial ( int n ) {

if ( n = = 0 ) return 1 ;

else return n * Factorial ( n - 1 ) ;

}

Compute Factorial (3) = 3!

k 3

F (3)

k 3

F (3)

n 3

F (3) 3* F (2)3* F (2)

nn 22

F(2)F(2) 2*2* F(1)F(1)

n 1

F (0) 1

F(1)F(1)

n 0

1*1* F(0)F(0)

F (1) = 1F (1) = 1F(2) = 2F(2) = 2F(3) = 6F(3) = 6Output 3! = 6Output 3! = 6

Page 89: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

// Compute Factorial

#include<iostream.h>

int Factorial ( int ) ;

void main ()

{ int k ;

cout << "Compute Factorial(k) , Please input k: " ;

cin >> k ;

cout << k << "! = " << Factorial(k) << endl ;

}

int Factorial ( int n ) {

if ( n == 0 )

return 1 ;

else

return n * Factorial ( n - 1 ) ;

}

Page 90: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

The generally format of recursive function

F ( x1 , x2 , … , xn ) if ( P1 ) E1

else if ( P2 ) E2

……

else if ( Pm ) Em

else Em+1

Pi ( i = 1, 2, …… , m ) is test expression

Ei ( i = 1, 2, …… , m +1 ) is expression

F() can presence within PF() can presence within Pi i and Eand Eii

Page 91: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examples

The mathematical definition of the nth Fibonacci number is

2nif

2nif

1nif

FFF

1F

1F

F

2n1nn

n

n

n

int Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Page 92: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)

Fibonacci (4) Fibonacci (3)

Fibonacci (3) Fibonacci (2)

Fibonacci (2) Fibonacci (1)

n stack

5432

Page 93: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)

Fibonacci (4) Fibonacci (3)

Fibonacci (3) Fibonacci (2)

Fibonacci (2) Fibonacci (1)

n stack

5432

1

Page 94: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)

Fibonacci (4) Fibonacci (3)

Fibonacci (3) Fibonacci (2)

Fibonacci (1)

n stack

543

1

1

Fibonacci (1)

Page 95: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)

Fibonacci (4) Fibonacci (3)

Fibonacci (3) Fibonacci (2)

Fibonacci (1)

n stack

543

1

1

Fibonacci (1)

1

Page 96: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)

Fibonacci (4) Fibonacci (3)

Fibonacci (3) Fibonacci (2)

n stack

543

2

1 1

Page 97: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)

Fibonacci (4) Fibonacci (3)

Fibonacci (2)

n stack

54

22

Fibonacci (2)

Page 98: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)

Fibonacci (4) Fibonacci (3)

Fibonacci (2)

n stack

54

22

Fibonacci (2)

1

Page 99: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)

Fibonacci (4) Fibonacci (3)

n stack

54

2

1

3

Page 100: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)

Fibonacci (3)

n stack

5

3

Fibonacci (3)

Fibonacci (1)Fibonacci (2) 32

Page 101: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)

Fibonacci (3)

n stack

5

3

Fibonacci (3)

Fibonacci (1)Fibonacci (2) 32

1

Page 102: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)

Fibonacci (3)

n stack

5

3

Fibonacci (3)

Fibonacci (1) 3

1

Fibonacci (1)1

Page 103: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)

Fibonacci (3)

n stack

5

3

Fibonacci (3)

Fibonacci (1) 3

1

Fibonacci (1)1

1

Page 104: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)

Fibonacci (3)

n stack

5

3

Fibonacci (3)

3

1 1

2

Page 105: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)n stack

5

3

2

5

Page 106: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)n stack

5

Page 107: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examplesint Fibonacci ( int n ) { if ( n <= 2 ) return 1 ; else return Fibonacci ( n-1 ) + Fibonacci ( n-2 ) ; }

Fibonacci (5)

Fibonacci (4) Fibonacci (3)

Fibonacci (3) Fibonacci (2) Fibonacci (1)Fibonacci (2)

Fibonacci (2) Fibonacci (1)

5

3 2

2 1

1 1

1 1

Page 108: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examples

// Input a string, output in opposite order

# include <iostream.h>

void reverse ()

{ char ch ; // local object

cin >> ch;

if ( ch != '.' )

reverse() ;

cout << ch ;

}

void main ()

{ cout << " Input a string : " << endl ;

reverse() ;

cout << endl ;

}

Page 109: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examples

// Revers decimal positive number

# include <iostream.h>

void reverse ( int n )

{ cout << n % 10 ; // output a last number at right

if ( n/10 != 0 )

reverse ( n/10 ); // compute quotient, recursive

}

void main ()

{ int k ;

cout << "Input a integer number( > 0 ) : "<< endl ;

cin >> k ;

reverse ( k ) ;

cout << endl ;

}

Page 110: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examples

// Hanoi tower

A B C

Page 111: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examples

// Hanoi tower

A B C

Page 112: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examples

// Hanoi tower

A B C

Page 113: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examples

// Hanoi tower

A B C

Page 114: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examples

// Hanoi tower

A B C

Page 115: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examples

// Hanoi tower

A B C

Page 116: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examples

// Hanoi tower

A B C

Page 117: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examples

// Hanoi tower # include < iostream.h >void hanoi ( int n, char a, char b, char c ) { if ( n >= 1 ) { hanoi ( n-1, a, c, b ) ; cout << a << " --> " << c << endl ; hanoi ( n-1, b, a, c ) ; } }void main () { int m ; cout << " Input the number of diskes: " << endl ; cin >> m ; hanoi ( m, 'A' , 'B' , 'C' ) ; }

Page 118: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examples// Hanoi tower void hanoi ( int n, char a, char b, char c ) { if ( n >= 1 ) { hanoi ( n-1, a, c, b) ; cout<<a<<" --> "<<c<<endl; hanoi (n-1, b, a, c) ; } }

Stack

n a b cn a b c

H ( 3, A, B, C )

3 A B C

H ( 2, A, C, B )2 A C B

H ( 1, A, B, C )

1 A B C

Output

A CH(3,A,B,C)

H(n-1,a,c,b)

H(n-1,a,c,b)

cout

Page 119: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examples// Hanoi tower void hanoi ( int n, char a, char b, char c ) { if ( n >= 1 ) { hanoi ( n-1, a, c, b) ; cout<<a<<" --> "<<c<<endl; hanoi (n-1, b, a, c) ; } }

Stack

n a b cn a b c

H ( 3, A, B, C )

3 A B C

H ( 2, A, C, B )2 A C B

H ( 1, A, B, C )

1 A B C

Output

A C

A B

H(3,A,B,C)

H(n-1,a,c,b)

H(n-1,a,c,b)

cout

Page 120: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examples// Hanoi tower void hanoi ( int n, char a, char b, char c ) { if ( n >= 1 ) { hanoi ( n-1, a, c, b) ; cout<<a<<" --> "<<c<<endl; hanoi (n-1, b, a, c) ; } }

Stack

n a b cn a b c

H ( 3, A, B, C )

3 A B C

H ( 2, A, C, B )2 A C B

Output

A C

A B

H(3,A,B,C)

H(n-1,a,c,b)

H ( 1, C, A, B )

1 C A B

H(n-1,b,a,c)

C B

cout

Page 121: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examples// Hanoi tower void hanoi ( int n, char a, char b, char c ) { if ( n >= 1 ) { hanoi ( n-1, a, c, b) ; cout<<a<<" --> "<<c<<endl; hanoi (n-1, b, a, c) ; } }

Stack

n a b cn a b c

H ( 3, A, B, C )

3 A B C

H ( 2, A, C, B )2 A C B

Output

A C

A B

H(3,A,B,C)

H(n-1,a,c,b)

H ( 1, C, A, B )

1 C A B

H(n-1,b,a,c)

C B

Page 122: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examples// Hanoi tower void hanoi ( int n, char a, char b, char c ) { if ( n >= 1 ) { hanoi ( n-1, a, c, b) ; cout<<a<<" --> "<<c<<endl; hanoi (n-1, b, a, c) ; } }

Stack

n a b cn a b c

H ( 3, A, B, C )

3 A B C

H ( 2, A, C, B )2 A C B

Output

A C

A B

H(3,A,B,C)

H(n-1,a,c,b) C B

A C

cout

Page 123: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examples// Hanoi tower void hanoi ( int n, char a, char b, char c ) { if ( n >= 1 ) { hanoi ( n-1, a, c, b) ; cout<<a<<" --> "<<c<<endl; hanoi (n-1, b, a, c) ; } }

Stack

n a b cn a b c

H ( 3, A, B, C )

3 A B C

Output

A C

A B

H(3,A,B,C)

C BH ( 2, B, A, C )2 B A C

H ( 1, B, C, A )

1 B C AH(n-1,b,a,c)

H(n-1,a,c,b)

A C

B A

cout

Page 124: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examples// Hanoi tower void hanoi ( int n, char a, char b, char c ) { if ( n >= 1 ) { hanoi ( n-1, a, c, b) ; cout<<a<<" --> "<<c<<endl; hanoi (n-1, b, a, c) ; } }

Stack

n a b cn a b c

H ( 3, A, B, C )

3 A B C

Output

A C

A B

H(3,A,B,C)

C BH ( 2, B, A, C )2 B A C

H ( 1, B, C, A )

1 B C AH(n-1,b,a,c)

H(n-1,a,c,b)

A C

B A

B C

cout

Page 125: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examples// Hanoi tower void hanoi ( int n, char a, char b, char c ) { if ( n >= 1 ) { hanoi ( n-1, a, c, b) ; cout<<a<<" --> "<<c<<endl; hanoi (n-1, b, a, c) ; } }

Stack

n a b cn a b c

H ( 3, A, B, C )

3 A B C

Output

A C

A B

H(3,A,B,C)

C BH ( 2, B, A, C )2 B A C

H(n-1,b,a,c)A C

B AH ( 1, A, B, C )

1 A B C

H(n-1,b,a,c) B C

A Ccout

Page 126: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examples// Hanoi tower void hanoi ( int n, char a, char b, char c ) { if ( n >= 1 ) { hanoi ( n-1, a, c, b) ; cout<<a<<" --> "<<c<<endl; hanoi (n-1, b, a, c) ; } }

Stack

n a b cn a b c

H ( 3, A, B, C )

3 A B C

Output

A C

A B

H(3,A,B,C)

C BH ( 2, B, A, C )2 B A C

H(n-1,b,a,c)A C

B AH ( 1, A, B, C )

1 A B C

H(n-1,b,a,c) B C

A C

Page 127: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examples// Hanoi tower void hanoi ( int n, char a, char b, char c ) { if ( n >= 1 ) { hanoi ( n-1, a, c, b) ; cout<<a<<" --> "<<c<<endl; hanoi (n-1, b, a, c) ; } }

Stack

n a b cn a b c

H ( 3, A, B, C )

3 A B C

Output

A C

A B

H(3,A,B,C)

C BH ( 2, B, A, C )2 B A C

H(n-1,b,a,c)A C

B A

B C

A C

Page 128: CHAPTER 5 Programmer-defined function 程序员定义的函数 Instroduction In this chapter, we consider the basics of programmer-defined functions with value parameters.

Other examplesOther examples// Hanoi tower void hanoi ( int n, char a, char b, char c ) { if ( n >= 1 ) { hanoi ( n-1, a, c, b) ; cout<<a<<" --> "<<c<<endl; hanoi (n-1, b, a, c) ; } }

Stack

n a b cn a b c

H ( 3, A, B, C )

3 A B C

Output

A C

A B

H(3,A,B,C)

C B

A C

B A

B C

A C

Over