計算機程式 第五單元 Function II 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to Program, 7/e,

Slides:



Advertisements
Similar presentations
Operations Management Unit 8: Planning Capacity 授課教師: 國立臺灣大學工商管理學系 黃崇興 教授 本課程指定教材為 Operations Management: Processes and Supply Chains, 10th ed., Lee J.
Advertisements

Inventory Management: Safety Inventory (II) 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 第七單元: Inventory Management:
Operations Management Unit 7: Managing Quality (2) 授課教師: 國立臺灣大學工商管理學系 黃崇興 教授 本課程指定教材為 Operations Management: Processes and Supply Chains, 10th ed., Lee.
Andy Guo Planning Demand and Supply in a Supply Chain 第二單元 (3) : Planning Demand and Supply in a Supply Chain 郭瑞祥教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣.
Inventory Management: Safety Inventory ( I ) 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 第六單元: Inventory Management:
Operations Management Unit 11: Managing Inventories (2) 授課教師: 國立臺灣大學工商管理學系 黃崇興 教授 本課程指定教材為 Operations Management: Processes and Supply Chains, 10th ed.,
Pricing and Revenue Management in the Supply Chain 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 第十二單元 (2) : Pricing.
Determining Optimal Level of Product Availability Optimal Matching of Supply and Demand (III) 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC.
新生講座 8.Reading Critically : Be a critical reader
1 Self Knowledge 自我知識 第六單元: Historical background: Locke 歷史背景 : 洛克 梁益堉 教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣.
Designing the Distribution Network in a Supply Chain 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 第十二單元 (3) : Designing.
佛學概論 第七講:《般若經》的佛法教學 授課教師:國立臺灣大學哲學系 蔡耀明 教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」臺灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」臺灣 3.0 版 1.
計算機程式 第十一單元 Operator Overloading I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to.
Designing the Distribution Network in a Supply Chain 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 第十 三單元 (1) :
Inventory Management: Cycle Inventory 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 第四單元: Inventory Management:
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
授課教師:楊宏智教授 1 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用.
Sourcing Decisions in a Supply Chain 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 第十一單元 : Sourcing Decisions in.
1 西方文明史 第二講: 兩河與埃及 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」臺灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」臺灣 3.0 版 劉 慧 教授.
Supply Chain Coordination & Information Integration 》【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 第七單元 (2) : Supply.
Andy Guo Planning Demand and Supply in a Supply Chain 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 第三單元 (1) : Planning.
計算機程式 第十四單元 Polymorphism 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to Program, 7/e,
Operations Management Unit 10: Managing Inventories (1) 授課教師: 國立臺灣大學工商管理學系 黃崇興 教授 本課程指定教材為 Operations Management: Processes and Supply Chains, 10th ed.,
計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to Program, 7/e,
Determining Optimal Level of Product Availability Optimal Matching of Supply and Demand (III) 1 蔣明晃教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Recursion Review.
Programming in C++ Language ( ) Lecture 6: Functions-Part2 Dr. Lubna Badri.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
Managing Transportation in a Supply Chain 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 第十一單元 (2) : Managing Transportation.
計算機程式 第一單元 Introduction to Computers, the Internet and the WWW 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣.
Strategic Framework to Analyze Supply Chains 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 第一單元: Strategic Framework.
Determining Optimal Level of Product Availability Optimal Matching of Supply and Demand (III) 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC.
 2008 Pearson Education, Inc. All rights reserved Function Call Stack and Activation Records Data structure: collection of related data items.
Functions and an Introduction to Recursion.  Recursive function ◦ A function that calls itself, either directly, or indirectly (through another function)
1 Self Knowledge 自我知識 第十三單元: Acquaintance theories: objections and replies 親知理論的困難與回應 梁益堉 教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用.
Legal and legislative crisis in EU and Asia : analysis and reform models? 【本著作除另有註明外,採取創用 CC 「姓名標 示-非商業性-相同方式分享」台灣 3.0 版授權 釋出】創用 CC 「姓名標 示-非商業性-相同方式分享」台灣.
LI & FUNG 【本著作除另有註明外,採取創用 CC 「姓名標示-非商業性-相 同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示-非商業性-相 同方式分享」台灣 3.0 版 郭瑞祥教授 第二單元: Case Study- Li & Fung Lifung group 1.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions (Recursion) Outline 5.13Recursion 5.14Example.
Lecture-13 Instructor Name: Muhammad Safyan Programming Fundamental.
1 西方文明史 第六講: 羅馬(二) 劉 慧 教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」臺灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」臺灣 3.0 版.
Inventory Management: Cycle Inventory-II 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 第五單元: Inventory Management:
西方文明史 第一講: 城市與文明 1 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」臺灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」臺灣 3.0 版 劉 慧 教授.
Determining Optimal Level of Product Availability Optimal Matching of Supply and Demand (III) 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC.
Chap. 12 The Health Benefits of Recreational Activities on Leisure Farms in Taiwan (III) 張俊彥 Chun-Yen Chang 國立臺灣大學園藝學系教授 國立臺灣大學園藝暨景觀學系 【園藝療法 】 【本著作除另有註明外,採取創用.
1 由佛典選讀入手之二 巴利經典選讀 第六講:由佛典選讀入手之二 巴利經典選讀 授課教師:國立臺灣大學哲學系 蔡耀明 教授 1 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」臺灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」臺灣 3.0 版.
計算機程式 第二單元 Control Structure I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to Program,
西方文明史 第十五講: 英法百年戰爭 劉 慧 教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」臺灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」臺灣 3.0 版 1.
計算機程式 第七單元 Pointers and Strings 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to Program,
Operations Management Unit 9: Managing Process Constraints 授課教師: 國立臺灣大學工商管理學系 余峻瑜 教授 本課程指定教材為 Operations Management: Processes and Supply Chains, 10th.
第十六單元:歐盟法與生命對話 歐盟法研討會 列強權力衝突間的緩衝區政經策略 EU Conflict Management, the Strategy of Politics and Economics in “Neutral Area” within Conflict of Super Powers.
Survey of Selected Western Classics Unit 2: A Rose for Emily 授課教師 : 李欣穎副教授 【本著作除另有註明外,採取創 CC 「姓名標示- 非商業性-相同方式分享」台灣 3.0 版授權釋出】創 CC 「姓名標示- 非商業性-相同方式分享」台灣.
計算機程式 第十三單元 Inheritance 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to Program, 7/e,
Ch15-1 Language Policy Based on Language Policy and Multiculturalism 語言政策的多元文化思考 授課老師:蘇以文 I-wen Su 【本著作除另有註明外,採取創用 CC 「姓名標示- 非商業性-相同方式分享」台灣 3.0 版授權釋出】
Ch3-2. Language and Human Migration 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】 The “Work” under the Creative Commons Taiwan 3.0 License of “BY-NC-SA”.
Creating and Negotiating Peace Structures: Difficulties for Small Nations Caught Between Superpowers Part 2: Difficulties with Economic Peace Structures.
計算機程式 第十二單元 Operator Overloading II 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版授權釋出】創用 CC 「姓名標示 -非商業性-相同方式分享」台灣 3.0 版 本課程指定教材為 C++ How to.
Operations Management Unit 1: Using Operations to Compete 授課教師: 國立臺灣大學工商管理學系 黃崇興 教授 本課程指定教材為 Operations Management: Processes and Supply Chains, 10th ed.,
Operations Management Unit 12: Designing Effective Supply Chains 授課教師: 國立臺灣大學工商管理學系 余峻瑜 教授 本課程指定教材為 Operations Management: Processes and Supply Chains,
歐盟法與生命文化 ( 二 ) 第一單元 授課教師: Dr. Lukas Lien, University of Hamburg, Germany European Union as Paradigm of globalization-In mirror of German Thinking Tradition.
歐盟法與生命文化 (二) 第七單元 EU finance policies and crisis management Ideas for a new strategy of growth for Europe and Asia! 授課教師: Mr. Dieter Lutzenberger 1 【本著作除另有註明外,採取創用.
歐盟法與生命文化 ( 二 ) 第二單元 Legal and legislative crisis in EU and Asia : analysis and reform models? 授課教師: Dr. Claudius Petzold 德國檢察官 1 【本著作除另有註明外,採取創用 CC 「姓名標示-非.
後現代主義藝術 I : 對藝術機制的批判 ─ 觀念、地景與表演藝術 劉瑞琪 本著作除另有註明外,採取創用 CC 「姓名標示-非 商業性-相同方式分享」台灣 2.5 版授權釋出創用 CC 「姓名標示-非 商業性-相同方式分享」台灣 2.5 版.
歐盟法與生命文化 (二) 第九單元 Germany’s dual vocational training system: a model for other countries? 授課教師: Mr. Raoul Kubitschek Christian Mahnke Manager Science &
 2006 Pearson Education, Inc. All rights reserved Functions and an Introduction to Recursion.
實驗經濟學一:行為賽局論 Experimental Economics I: Behavioral Game Theory 補充材料:於現實驗證賽局理論:Swedish LUPI Lottery 賽局 Additional Material: “Testing Game Theory in.
Aristotle I Man as a Political Animal
RECURSION.
6.11 Function Call Stack and Activation Records
實驗經濟學一:行為賽局論 Experimental Economics I: Behavioral Game Theory 第九講:學習理論:制約、計牌與EWA Lecture 9: Learning: Reinforcement, Fictitious Play, and EWA 授課教師:國立臺灣大學.
Introduction to Digital Speech Processing
Presentation transcript:

計算機程式 第五單元 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 服務合約

n! int F (int n) { int tmp=1; for(int i=n;i>=1;i--) tmp*=i; return tmp; } 2

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

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

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

Recursion (cont.) 6

7 Fig.6.29 p.255~256

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;

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

Recursion (cont.) 10 Fig.6.30 p.257~258

Recursion (cont.) 11

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

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

References (cont.) 14 p

References (cont.) 15 p.244

References (cont.) 16 p

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

Function Overloading (cont.) 18 p

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

Function Template (cont.) 20 p.251

21 p

版權聲明 22 頁碼作品版權圖示來源 / 作者 1-22 本作品轉載自 Microsoft Office 2007 多媒體藝廊,依據 Microsoft 服務合約及著作權法第 46 、 52 、 65 條合理使用。 2 Open Clip Art Library ,作者: Bart Massey ,本作品轉載自: , 瀏覽日期: 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 、 、 18 、 Open Clip Art Library ,作者: aritztg ,本作品轉載自: ,瀏覽日期: 2013/1/10 。 9 Open Clip Art Library ,作者: Alexey Zharov ,本作品轉載自: ,瀏覽日期: 2013/1/20 。 11 C++ How to Program, 7/e ,作者: Harvey M. Deitel and Paul J. Deitel , 出版社: Deitel & Associates ,出版日期: 2010 , P.258 。 依據著作權法第 46 、 52 、 65 條合理使用。