1 chapter 5 functions c/c++ language programming wanxiang che

49
1 Chapter 5 Functions C/C++ Language Programming Wanxiang Che

Upload: gertrude-barnett

Post on 28-Dec-2015

228 views

Category:

Documents


4 download

TRANSCRIPT

1

Chapter 5 Functions

C/C++ Language Programming

Wanxiang Che

Over!

• Any programming logic problem can be solved using an appropriate combination of only three programming structures– Sequence– Selection– Loop

• We can write all codes in main function

2

3

大话三国

懿:孔明寝食及事之烦简若何?

使者:丞相夙兴夜寐,罚二十以上皆亲览焉。所啖之食,日不过数升。

懿:孔明食少事烦,其能久乎?

4

int n = 4, m = 2;int Pn = 1, Pnm = 1, Pm = 1;

// compute n!for (int i = 1; i <= n; i++) Pn = Pn * i;// compute (n – m)!for (int i = 1; i <= n - m; i++) Pnm = Pnm * i;// compute m!for (int i = 1; i <= m; i++) Pm = Pm * i;int result = Pn / (Pnm * Pm);

!

( )! !mn

nC

n m m

孔明做法

5

A function is a collection of statements that are grouped together to perform an operation.

int P(int x)

{

int result = 1;

for (int i = 1; i <= x; i++)

result = result * i;

return result;

}

Return value type

Function name

Parameter list

Return value

Function body

What’s Function?

6

Using Functionsint n = 4, m = 2;int Pn = 1, Pnm = 1, Pm = 1;

// compute n!for (int i = 1; i <= n; i++) Pn = Pn * i;// compute (n – m)!for (int i = 1; i <= n - m; i++) Pnm = Pnm * i;// compute m!for (int i = 1; i <= m; i++) Pm = Pm * i;int result = Pn / (Pnm * Pm);

Pn = P(n)

Pm = P(m)

Pnm = P(n - m)

Arguments

7

Calling Function

int n = 4, m = 2;

int Pn , Pnm, Pm;

Pn = P(n);

Pnm = P(n – m);

Pm = P(m);

int result = Pn / (Pnm * Pm);

int P(int x)

{

int result = 1;

for (int i = 1; i <= x; i++)

result *= i;

return result;

}

8

Displaying Prime Numbers

Problem: Write a program that displays the first 50 prime numbers in five lines, each of which contains 10 numbers. An integer greater than 1 is prime if its only positive divisor is 1 or itself.

Solution: The problem can be broken into the following tasks:

•Determine whether a given number is prime.•For number = 2, 3, 4, 5, 6, ..., test whether the number is prime.•Count the prime numbers.•Print each prime number, and print 10 numbers per line.

9

Swap two numbers with Function

void swap(int n1, int n2) {

int temp;

temp = n1;

n1 = n2;

n2 = temp;

}

main() {

int num1 = 1;

int num2 = 2;

swap(num1, num2);

cout << num1 << " " << num2 << endl;

}

10

void swap(int n1, int n2){

int temp;

temp = n1;

n1 = n2;

n2 = temp;

}

22111num1 num2

2

n1 n2num1 num2

n1 n2

Pass by Valuemain() {

int num1 = 1;

int num2 = 2;

swap(num1, num2);

cout << num1 << " " << num2;

}

陈水扁 马英九Pass by Value

11

Reference Variables

• A reference variable is an alias for another variable– 阿扁 == 陈水扁

• Any changes made through the reference variable are actually performed on the original variable

• Declare a reference variable, place the & in front of the alias– int x;

– int &y = x;

• Reference variable can be used as a function parameter to reference the original argument

12

void swap(int &n1, int &n2){

int temp;

temp = n1;

n1 = n2;

n2 = temp;

}

1num1 num2

2

n1 n2num1 num2

n1 n2

Pass by Referencemain() {

int num1 = 1;

int num2 = 2;

swap(num1, num2);

cout << num1 << " " << num2;

}

陈水扁 阿扁

13

Overloading Functions

• Another function with the same name but different parameters

• The previous swap function works only with the int data type

• Using overloading function to find which of two floating-point numbers has the maximum value

14

Scope of Variables

• Scope– The part of the program where the

variable can be referenced.

• The scope of a variable starts from its declaration and continues to the end of the block that contains the variable.

15

Global Variables

• Declared outside all functions and are accessible to all functions in its scope.

• Local variables do not have default values, but global variables are defaulted to zero.

16

Static Local Variables

• Static local variables are permanently allocated in the memory for the lifetime of the program.– static int x;

• After a function completes its execution, all its local variables are destroyed.

• The static local variables are retained and can be reused in the next call.

17

Function Abstraction

You can think of the Function body as a black box that contains the detailed implementation for the Function.

Method Signature

Method body

Black Box

Optional arguments for Input

Optional return value

18

Benefits of Functions

• Write a Function once and reuse it anywhere.

• Information hiding. Hide the implementation from the user.

• Reduce complexity– Divide and Conquer

19

Guess Number

• 先由计算机“想”一个 1 到 100 之间的数请人猜,如果猜对了,显示“正确!”,并赞美一番;否则显示“错误!”,并提示所猜的数是大了还是小了。最多可以猜 7次。如 7 次仍未猜中,则停止本次猜数,并羞辱一番游戏者。每次运行程序可以反复猜多个数,直到游戏者想停止时才结束。

20

主功能 猜数字开始开始

结束结束

生成数字生成数字

猜数字猜数字

是否继续?是否继续?

NN

YY

开始开始

结束结束

猜得对吗?猜得对吗?NN

YY

提示大小提示大小

次数次数 <7<7 ??

输入数字输入数字

NN

YY

21

Recursion

22

Computing Factorialfactorial(0) = 1;

factorial(n) = n*factorial(n-1);

23

Computing Factorial

factorial(3)

factorial(0) = 1;

factorial(n) = n*factorial(n-1);

24

Computing Factorial

factorial(3) = 3 * factorial(2)

factorial(0) = 1;

factorial(n) = n*factorial(n-1);

25

Computing Factorial

factorial(3) = 3 * factorial(2)

= 3 * (2 * factorial(1))

factorial(0) = 1;

factorial(n) = n*factorial(n-1);

26

Computing Factorial

factorial(3) = 3 * factorial(2)

= 3 * (2 * factorial(1))

= 3 * ( 2 * (1 * factorial(0)))

factorial(0) = 1;

factorial(n) = n*factorial(n-1);

27

Computing Factorial

factorial(3) = 3 * factorial(2)

= 3 * (2 * factorial(1))

= 3 * ( 2 * (1 * factorial(0)))

= 3 * ( 2 * ( 1 * 1)))

factorial(0) = 1;

factorial(n) = n*factorial(n-1);

28

Computing Factorial

factorial(3) = 3 * factorial(2)

= 3 * (2 * factorial(1))

= 3 * ( 2 * (1 * factorial(0)))

= 3 * ( 2 * ( 1 * 1)))

= 3 * ( 2 * 1)

factorial(0) = 1;

factorial(n) = n*factorial(n-1);

29

Computing Factorial

factorial(3) = 3 * factorial(2)

= 3 * (2 * factorial(1))

= 3 * ( 2 * (1 * factorial(0)))

= 3 * ( 2 * ( 1 * 1)))

= 3 * ( 2 * 1)

= 3 * 2

factorial(0) = 1;

factorial(n) = n*factorial(n-1);

30

Computing Factorial

factorial(3) = 3 * factorial(2) = 3 * (2 * factorial(1)) = 3 * ( 2 * (1 * factorial(0))) = 3 * ( 2 * ( 1 * 1))) = 3 * ( 2 * 1) = 3 * 2 = 6

factorial(0) = 1;

factorial(n) = n*factorial(n-1);

31

Trace Recursive factorial

return 1

factorial(4)

return 4 * factorial(3)

return 3 * factorial(2)

return 2 * factorial(1)

return 1 * factorial(0)

Step 9: return 24 Step 0: executes factorial(4)

Step 1: executes factorial(3)

Step 2: executes factorial(2)

Step 3: executes factorial(1)

Step 5: return 1

Step 6: return 1

Step 7: return 2

Step 8: return 6

Step 4: executes factorial(0)

Executes factorial(4)

Main method

Stack

32

Trace Recursive factorial

return 1

factorial(4)

return 4 * factorial(3)

return 3 * factorial(2)

return 2 * factorial(1)

return 1 * factorial(0)

Step 9: return 24 Step 0: executes factorial(4)

Step 1: executes factorial(3)

Step 2: executes factorial(2)

Step 3: executes factorial(1)

Step 5: return 1

Step 6: return 1

Step 7: return 2

Step 8: return 6

Step 4: executes factorial(0)

Executes factorial(3)

Main method

Space Required for factorial(4)

Stack

33

Trace Recursive factorial

return 1

factorial(4)

return 4 * factorial(3)

return 3 * factorial(2)

return 2 * factorial(1)

return 1 * factorial(0)

Step 9: return 24 Step 0: executes factorial(4)

Step 1: executes factorial(3)

Step 2: executes factorial(2)

Step 3: executes factorial(1)

Step 5: return 1

Step 6: return 1

Step 7: return 2

Step 8: return 6

Step 4: executes factorial(0)

Executes factorial(2)

Main method

Space Required for factorial(4)

Space Required for factorial(3)

Stack

34

Trace Recursive factorial

return 1

factorial(4)

return 4 * factorial(3)

return 3 * factorial(2)

return 2 * factorial(1)

return 1 * factorial(0)

Step 9: return 24 Step 0: executes factorial(4)

Step 1: executes factorial(3)

Step 2: executes factorial(2)

Step 3: executes factorial(1)

Step 5: return 1

Step 6: return 1

Step 7: return 2

Step 8: return 6

Step 4: executes factorial(0)

Executes factorial(1)

Main method

Space Required for factorial(4)

Space Required for factorial(3)

Space Required for factorial(2)

Stack

35

Trace Recursive factorial

return 1

factorial(4)

return 4 * factorial(3)

return 3 * factorial(2)

return 2 * factorial(1)

return 1 * factorial(0)

Step 9: return 24 Step 0: executes factorial(4)

Step 1: executes factorial(3)

Step 2: executes factorial(2)

Step 3: executes factorial(1)

Step 5: return 1

Step 6: return 1

Step 7: return 2

Step 8: return 6

Step 4: executes factorial(0)

Executes factorial(0)

Main method

Space Required for factorial(4)

Space Required for factorial(3)

Space Required for factorial(2)

Space Required for factorial(1)

Stack

36

Trace Recursive factorial

return 1

factorial(4)

return 4 * factorial(3)

return 3 * factorial(2)

return 2 * factorial(1)

return 1 * factorial(0)

Step 9: return 24 Step 0: executes factorial(4)

Step 1: executes factorial(3)

Step 2: executes factorial(2)

Step 3: executes factorial(1)

Step 5: return 1

Step 6: return 1

Step 7: return 2

Step 8: return 6

Step 4: executes factorial(0)

returns 1

Main method

Space Required for factorial(4)

Space Required for factorial(3)

Space Required for factorial(2)

Space Required for factorial(1)

Space Required for factorial(0)

Stack

37

Trace Recursive factorial

return 1

factorial(4)

return 4 * factorial(3)

return 3 * factorial(2)

return 2 * factorial(1)

return 1 * factorial(0)

Step 9: return 24 Step 0: executes factorial(4)

Step 1: executes factorial(3)

Step 2: executes factorial(2)

Step 3: executes factorial(1)

Step 5: return 1

Step 6: return 1

Step 7: return 2

Step 8: return 6

Step 4: executes factorial(0)

returns factorial(0)

Main method

Space Required for factorial(4)

Space Required for factorial(3)

Space Required for factorial(2)

Space Required for factorial(1)

Stack

38

Trace Recursive factorial

return 1

factorial(4)

return 4 * factorial(3)

return 3 * factorial(2)

return 2 * factorial(1)

return 1 * factorial(0)

Step 9: return 24 Step 0: executes factorial(4)

Step 1: executes factorial(3)

Step 2: executes factorial(2)

Step 3: executes factorial(1)

Step 5: return 1

Step 6: return 1

Step 7: return 2

Step 8: return 6

Step 4: executes factorial(0)

returns factorial(1)

Main method

Space Required for factorial(4)

Space Required for factorial(3)

Space Required for factorial(2)

Stack

39

Trace Recursive factorial

return 1

factorial(4)

return 4 * factorial(3)

return 3 * factorial(2)

return 2 * factorial(1)

return 1 * factorial(0)

Step 9: return 24 Step 0: executes factorial(4)

Step 1: executes factorial(3)

Step 2: executes factorial(2)

Step 3: executes factorial(1)

Step 5: return 1

Step 6: return 1

Step 7: return 2

Step 8: return 6

Step 4: executes factorial(0)

returns factorial(2)

Main method

Space Required for factorial(4)

Space Required for factorial(3)

Stack

40

Trace Recursive factorial

return 1

factorial(4)

return 4 * factorial(3)

return 3 * factorial(2)

return 2 * factorial(1)

return 1 * factorial(0)

Step 9: return 24 Step 0: executes factorial(4)

Step 1: executes factorial(3)

Step 2: executes factorial(2)

Step 3: executes factorial(1)

Step 5: return 1

Step 6: return 1

Step 7: return 2

Step 8: return 6

Step 4: executes factorial(0)

returns factorial(3)

Main method

Space Required for factorial(4)

Stack

41

Trace Recursive factorial

return 1

factorial(4)

return 4 * factorial(3)

return 3 * factorial(2)

return 2 * factorial(1)

return 1 * factorial(0)

Step 9: return 24 Step 0: executes factorial(4)

Step 1: executes factorial(3)

Step 2: executes factorial(2)

Step 3: executes factorial(1)

Step 5: return 1

Step 6: return 1

Step 7: return 2

Step 8: return 6

Step 4: executes factorial(0)

returns factorial(4)

Main method

Stack

42

Fibonacci Numbers

• 兔子繁殖问题• Fibonacci 数列

– 0, 1, 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 , 144 ,233 ,…

1

2

1 2

0 ( 1)

1 ( 2)

( 3)n n n

f n

f n

f f f n

43

Fibonnaci Numbers, cont.

return fib(3) + fib(2)

return fib(2) + fib(1)

return fib(1) + fib(0)

return 1

return fib(1) + fib(0)

return 0

return 1

return 1 return 0

1: call fib(3)

2: call fib(2)

3: call fib(1)

4: return fib(1)

7: return fib(2)

5: call fib(0)

6: return fib(0)

8: call fib(1)

9: return fib(1)

10: return fib(3) 11: call fib(2)

16: return fib(2)

12: call fib(1) 13: return fib(1) 14: return fib(0)

15: return fib(0)

fib(4) 0: call fib(4) 17: return fib(4)

44

Characteristics of Recursion

• Break a problem into subproblems. If a subproblem resembles the original problem, you can apply the same approach to solve the subproblem recursively.– One or more base cases (the simplest case)

are used to stop recursion.– Every recursive call reduces the original

problem, bringing it increasingly closer to a base case until it becomes that case.

45

Problem Solving Using Recursion

void nPrint(int times) {

if (times >= 1) {

cout << "Hello World!" << endl;

nPrint(times - 1);

} // The base case is n == 0

}

• Printing a message for n times

• Break the problem into two subproblems:1. Print the message one time

2. Print the message for n-1 times

46

Towers of Hanoi

• There are n disks labeled 1, 2, 3, . . ., n, and three towers labeled A, B, and C.

• No disk can be on top of a smaller disk at any time.

• All the disks are initially placed on tower A, the target tower is B.

• Only one disk can be moved at a time, and it must be the top disk on the tower.

47

Towers of Hanoi, cont.

A B

Original position

C A B

Step 4: Move disk 3 from A to B

C

A B

Step 5: Move disk 1 from C to A

C A B

Step 1: Move disk 1 from A to B

C

A C B

Step 2: Move disk 2 from A to C

A B

Step 3: Move disk 1 from B to C

C A B

Step 7: Mve disk 1 from A to B

C

A B

Step 6: Move disk 2 from C to B

C

48

Solution to Towers of Hanoi

A B

Original position

C

.

.

.

A B

Step 1: Move the first n-1 disks from A to C recursively

C

.

.

.

A B

Step2: Move disk n from A to C

C

.

.

.

A B

Step3: Move n-1 disks from C to B recursively

C

.

.

.

n-1 disks

n-1 disks

n-1 disks

n-1 disks

The Towers of Hanoi problem can be decomposed into three subproblems.

49

Solution to Towers of Hanoi

• Move the first n - 1 disks from A to C with the assistance of tower B.

• Move disk n from A to B.• Move n - 1 disks from C to B with the assistance of tower A.