Download presentation
Presentation is loading. Please wait.
Published byAllyson Martin Modified over 9 years ago
1
計算機程式 第五單元 Function II 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to Program, 7/e, Harvey M. Deitel and Paul J. Deitel, both from Deitel & Associates, Inc. © 2010 。 本 講義僅引用部分內容,請讀者自行準備。 1 本作品轉載自 Microsoft Office 2007 多媒體藝廊,依據 Microsoft 服務合約及著作權法 第 46 、 52 、 65 條合理使用。 Microsoft 服務合約
2
n! int F (int n) { int tmp=1; for(int i=n;i>=1;i--) tmp*=i; return tmp; } 2
3
Recursion Recursion function o A function that calls itself, either directly, or indirectly (through another function) Recursion o Base case(s) The simplest case(s), which the function knows how to handle o For all other cases, the function typically divides the problem into two conceptual pieces A piece that the function knows how to do A piece that it does not know how to do o Slightly simper or smaller version of the original problem 3
4
Recursion (cont.) Recursion (Cont.) o Recursive call (also called the recursion step) The function launches (calls) a fresh copy of itself to work on the smaller problem Can result in many more recursive calls, as the function keeps dividing each new problem into two conceptual pieces This sequence of smaller and smaller problems must eventually converge on the base case o Otherwise the recursion will continue forever 4
5
Recursion (cont.) Factorial o The factorial of a nonnegative integer n, written n!(and pronounced “n factorial”), is the product n ‧ (n-1) ‧ (n-2) ‧... ‧ 1 o Recursive definition of the factorial function n! = n ‧ (n-1)! Example o 5! = 5 ‧ 4 ‧ 3 ‧ 2 ‧ 1 o 5! = 5 ‧ (4 ‧ 3 ‧ 2 ‧ 1) o 5! = 5 ‧ (4 !) 5
6
Recursion (cont.) 6
7
7 Fig.6.29 p.255~256
8
Recursion (cont.) Q: F(5) = ? n=5 return 5*F(4); n=4 return 4*F(3); n=3 return 3*F(2); n=2 return 2*F(1); n=1 return 1; 8 2 6 24 120
9
Recursion (cont.) The Fibonacci series o 0,1,1,2,3,5,8,11,13,21,... o Begins with 0 and 1 o Each subsequent Fibonacci number is the sum of the previous two Fibonacci numbers o can be defined recursively as follows: F(0) = 0 F(1) = 1 F(n) = F(n-1) + F(n-2) 9
10
Recursion (cont.) 10 Fig.6.30 p.257~258
11
Recursion (cont.) 11
12
Recursion vs. Iteration Both are based on a control statement o Iteration – repetition structure o Recursion – selection structure Both involve repetition o Iteration – explicitly uses repetition structure o Recursion – repeated function calls Both involve a termination test o Iteration – loop-termination test o Recursion – base case Both gradually approach termination o Iteration modifies counter until loop-termination test fails o Recursion produces progressively simpler versions of problem Both can occur infinitely o Iteration – if loop-continuation condition never fails o Recursion – if recursion step does not simplify the problem 12
13
References Two ways to pass arguments to functions o Pass-by-value A copy of the argument’s value is passed to the called function Changes to the copy do not affect the original variable’s value in the caller o Prevents accidental side effects of functions o Pass-by-reference Gives called function the ability to access and modify the caller’s argument data directly 13
14
References (cont.) 14 p.243-244
15
References (cont.) 15 p.244
16
References (cont.) 16 p.242-243
17
Function Overloading Overloaded functions o Overloaded functions have Same name Different sets of parameters o Compiler selects proper function to execute based on number, types and order of arguments in the function call o Commonly used to create several functions of the same name that perform similar tasks, but on different data types 17
18
Function Overloading (cont.) 18 p.248-249
19
Function Template Function-template specializations o Generated automatically by the compiler to handle each type of call to the function template o Example for function template max with type parameter T called with int arguments o Compiler detects a max invocation in the program code int is substituted for T throughout the template definition This produces function-template specialization max 19
20
Function Template (cont.) 20 p.251
21
21 p.252-253
22
版權聲明 22 頁碼作品版權圖示來源 / 作者 1-22 本作品轉載自 Microsoft Office 2007 多媒體藝廊,依據 Microsoft 服務合約及著作權法第 46 、 52 、 65 條合理使用。 2 Open Clip Art Library ,作者: Bart Massey ,本作品轉載自: http://openclipart.org/detail/171135/computer-terminal-by-po8-171135 , http://openclipart.org/detail/171135/computer-terminal-by-po8-171135 瀏覽日期: 2013/1/20 。 6 C++ How to Program, 7/e ,作者: Harvey M. Deitel and Paul J. Deitel , 出版社: Deitel & Associates ,出版日期: 2010 , P.255 。 依據著作權法第 46 、 52 、 65 條合理使用。 7 、 10 、 14- 16 、 18 、 20-21 Open Clip Art Library ,作者: aritztg ,本作品轉載自: http://openclipart.org/detail/3422/mouse-by-aritztg ,瀏覽日期: 2013/1/10 。 http://openclipart.org/detail/3422/mouse-by-aritztg 9 Open Clip Art Library ,作者: Alexey Zharov ,本作品轉載自: http://openclipart.org/detail/20213/engineer-by-verses ,瀏覽日期: 2013/1/20 。 http://openclipart.org/detail/20213/engineer-by-verses 11 C++ How to Program, 7/e ,作者: Harvey M. Deitel and Paul J. Deitel , 出版社: Deitel & Associates ,出版日期: 2010 , P.258 。 依據著作權法第 46 、 52 、 65 條合理使用。
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.