Download presentation
Presentation is loading. Please wait.
Published byMegan Bruce Modified over 9 years ago
1
計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 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
Function: An Overview Why functions? o More manageable o Software use o Avoid repeated code Two types of functions o Standard library: e.g., math, string, character, I/O, error handling Fig.6.2 on page 211 Fig.6.7 on page 219~220 o User-defined function 2
3
User-Defined Function Function definition return_type fnu_name (para_list) { declaration; statements; return; } e.g., int square( int y ) { return y * y; } 3 Note: A function cannot be defined within another function.
4
User-Defined Function (cont.) Function prototype o return_type fun_name(para_list); o e.g., int square (int); double max (double, double, double); o Function definition vs. function prototype Ordering Relationship A must-be combination?? o Relationship among function definition, function prototype, and function call # include # include “my_header.h” 4
5
User-Defined Function: An Example (1) 5 1 4 9 16 25 36 49 64 81 100
6
User-Defined Function: An Example (1’) 6 1 4 9 16 25 36 49 64 81 100
7
User-Defined Function: An Example (2) 7
8
8
9
Function Return Return something: return expression; e.g., int square (int x) { return x*x; } Return nothing: return;} or } e.g., void print (int x) { cout<<“Hello… \n”; return; } 9 void main() {declarations; statements;}
10
Empty Parameter List 10 p.239
11
Function: An Overview (cont.) 11 C++ Standard Library header file Explanation Contains function prototypes for the C++ standard input and standard output functions, introduced in Chapter 2, and is covered in more detail in Chapter 15, Stream Input/Output. This header file replaces header file. Contains function prototypes for stream manipulators that format streams of data. This header file is first used in Section 4.9 and is discussed in more detail in Chapter 15, Stream Input/Output. This header file replaces header file. Contains function prototypes for math library functions (discussed in Section 6.3). This header file replaces header file.
12
Function: An Overview (cont.) 12 C++ Standard Library header file Explanation Contains function prototypes for conversions of numbers to text, text to numbers, memory allocation, random numbers and various other utility functions. Portions of the header file are covered in Section 6.7; Chapter 11, Operator Overloading; String and Array Objects; Chapter 16, Exception Handling; Chapter 19, Web Programming; Chapter 22, Bits, Characters, C- Strings and structs; and Appendix E, C Legacy Code Topics. This header file replaces header file. Contains function prototypes and types for manipulating the time and date. This header file replaces header file. This header file is used in Section 6.7. Contains function prototypes for C-style string-processing functions. This header file replaces header file. This header file is used in Chapter 11, Operator Overloading; String and Array Objects.
13
Function: An Overview (cont.) 13 C++ Standard Library header file Explanation,,,,,,, These header files contain classes that implement the C++ Standard Library containers. Containers store data during a program’s execution. The header is first introduced in Chapter 7, Arrays and Vectors. We discuss all these header files in Chapter 23, Standard Template Library (STL). Contains function prototypes for functions that test characters for certain properties (such as whether the character is a digit or a punctuation), and function prototypes for functions that can be used to convert lowercase letters to uppercase letters and vice versa. This header file replaces header file. These topics are discussed in Chapter 8, Pointers and Pointer- Based Strings, and Chapter 22, Bits, Characters, C-Strings and structs.
14
Function: An Overview (cont.) 14 C++ Standard Library header file Explanation Contains classes for runtime type identification (determining data types at execution time). This header file is discussed in Section 13.8., These header files contain classes that are used for exception handling (discussed in Chapter 16). Contains classes and functions used by the C++ Standard Library to allocate memory to the C++ Standard Library containers. This header is used in Chapter 16, Exception Handling. Contains function prototypes for functions that perform input from files on disk and output to files on disk (discussed in Chapter 17, File Processing). This header file replaces header file.
15
Function: An Overview (cont.) 15 C++ Standard Library header file Explanation Contains the definition of class string from the C++ Standard Library (discussed in Chapter 18). Contains function prototypes for functions that perform input from strings in memory and output to strings in memory (discussed in Chapter 18, Class string and String Stream Processing). Contains classes and functions used by C++ Standard Library algorithms. This header file is used in Chapter 23. Contains classes for accessing data in the C++ Standard Library containers. This header file is used in Chapter 23, Standard Template Library (STL).
16
Function: An Overview (cont.) 16 C++ Standard Library header file Explanation Contains functions for manipulating data in C++ Standard Library containers. This header file is used in Chapter 23. Contains macros for adding diagnostics that aid program debugging. This replaces header file from pre- standard C++. This header file is used in Appendix F, Preprocessor. Contains the floating-point size limits of the system. This header file replaces header file. Contains function prototypes for the C-style standard input/output library functions and information used by them. This header file replaces header file.
17
Function: An Overview (cont.) 17 C++ Standard Library header file Explanation Contains the integral size limits of the system. This header file replaces header file. Contains classes and functions normally used by stream processing to process data in the natural form for different languages (e.g., monetary formats, sorting strings, character presentation, etc.). Contains classes for defining the numerical data type limits on each computer platform. Contains classes and functions that are used by many C++ Standard Library header files.
18
Function: An Overview (cont.) How to use it? o Function call o Format function_name(argument list) 18 e.g., double x=9.0, y; y=sqrt(x); (+) write once, use many function call return value y=f(x) e.g., pow(x,y); sqrt(9); sqrt(x); sqrt(sqrt(x));
19
Function: An Example (1) 19 p.221 Note: using namespace std;
20
Function: An Example (2) 20 p.222-223
21
Function: An Example (3) 21 p.224-225
22
Function: An Overview (cont.) Control passing o Main program → function → Function call o Function → main program → Return 22 boss worker1 worker2worker3 worker4worker5
23
Function: An Overview (cont.) Coercion of arguments e.g., double square (double); int x; x = square (4); e.g., double a=1.0, b=5.8; int x=2, y=3, z=5; a = b+x+y; z = a+b; Promotion rule o Implicit promotion o Explicit promotion 23
24
Promotion Hierarchy 24 Data Types long double double float unsigned long int(synonymous with unsigned long) long int(synonymous with long) unsigned int(synonymous with unsigned) int unsigned short int(synonymous with unsigned short) short int(synonymous with short) unsigned char char bool
25
Storage Class and Scope Each identifier has several attributes o Name, type, size, and value o Storage class, scope, and linkage Storage class o Determine the period during which the identifier exists in memory, i.e., lifetime Scope o Determine where the identifier can be referenced in a program Linkage o Determines whether an identifier is known only in the source file where it is declared or across multiple files that are compiled, then linked together C++ provides five storage-class specifiers: o auto, register, extern, mutable and static o An identifier’s storage-class specifier helps determine its storage class and linkage 25
26
Storage Class and Scope (cont.) Storage class o Determine the lifetime for each variable o Automatic vs. static (auto, register) (extern, static) o e.g., auto int x; static double x; Scope o Local variable vs. global variable 26
27
Storage Class and Scope: An Example 27 p.233-234
28
Function Call Stack 28 p.236
29
Function Call Stack (cont.) 29 Step 1: Operating system invokes main to execute application. ___________________ Operating System Return location: R1 Automatic variables: 10 a Function call stack after Step 1 Return location R1 Top of stack Activation record for function main { int a = 10; cout<<a<<“squared:” <<square(a)<<endl; return 0; } int main()
30
Function Call Stack (cont.) 30 Step 2: main invokes function square to perform calculation. { int a = 10; cout<<a<<“squared:”<<square(a)<<endl; return 0; } int main() Return location: R1 Automatic variables: 10 a Function call stack after Step 2 Activation record for function main Top of stack Return location: R2 Automatic variables: 10 x Activation record for function square { return x*x; } int square (int x) Return location R2
31
Function Call Stack (cont.) 31 Step 3: square returns its results to main. { int a = 10; cout<<a<<“squared:”<<square(a)<<endl; return 0; } int main() Return location: R1 Automatic variables: 10 a Function call stack after Step 3 Activation record for function main Top of stack { return x*x; } int square (int x) Return location R2
32
Inline Function 32 p.240-241
33
Default Argument 33 p.245-246
34
版權聲明 34 頁碼作品版權圖示來源 / 作者 1-35 本作品轉載自 Microsoft Office 2007 多媒體藝廊,依據 Microsoft 服務合約及著作權法第 46 、 52 、 65 條合理使用。 2 Open Clip Art Library ,作者: leandrosciola@gmail.com ,本作品轉載自: http://openclipart.org/detail/64771/box-web-2.0-by-leandrosciola@gmail.com , http://openclipart.org/detail/64771/box-web-2.0-by-leandrosciola@gmail.com 瀏覽日期: 2013/1/21 。 5-8 臺灣大學電機系 廖婉君教授 10, 19-21, 27-28, 32- 33 Open Clip Art Library ,作者: aritztg ,本作品轉載自: http://openclipart.org/detail/3422/mouse-by-aritztg ,瀏覽日期: 2013/1/10 。 http://openclipart.org/detail/3422/mouse-by-aritztg 11-17 C++ How to Program, 7/e ,作者: Harvey M. Deitel and Paul J. Deitel , 出版社: Deitel & Associates ,出版日期: 2010 , P.219~220 。 依據著作權法第 46 、 52 、 65 條合理使用。
35
版權聲明 35 頁碼作品版權圖示來源 / 作者 22 C++ How to Program, 7/e ,作者: Harvey M. Deitel and Paul J. Deitel , 出版社: Deitel & Associates ,出版日期: 2010 , P.210 。 依據著作權法第 46 、 52 、 65 條合理使用。 24 C++ How to Program, 7/e ,作者: Harvey M. Deitel and Paul J. Deitel , 出版社: Deitel & Associates ,出版日期: 2010 , P.218 。 依據著作權法第 46 、 52 、 65 條合理使用。 26 Open Clip Art Library ,作者: Machovka ,本作品轉載自: http://openclipart.org/detail/2297/floppy-diskette-by-machovka ,瀏覽日期: 2013/1/21 。 http://openclipart.org/detail/2297/floppy-diskette-by-machovka 29-30 C++ How to Program, 7/e ,作者: Harvey M. Deitel and Paul J. Deitel , 出版社: Deitel & Associates ,出版日期: 2010 , P.237 。 依據著作權法第 46 、 52 、 65 條合理使用。 31 C++ How to Program, 7/e ,作者: Harvey M. Deitel and Paul J. Deitel , 出版社: Deitel & Associates ,出版日期: 2010 , P.238 。 依據著作權法第 46 、 52 、 65 條合理使用。
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.