Upload
megan-bruce
View
227
Download
0
Embed Size (px)
Citation preview
1
計算機程式第四單元 Function I
授課教師:廖婉君教授
【 本 著 作 除 另 有 註 明 外 , 採 取 創用 CC
「姓名標示-非商業性-相同方式分享」台灣 3.0
版授權釋出】本課程指定教材為 C++ How to Program, 7/e, Harvey M. Deitel and Paul J. Deitel, both from Deitel & Associates, Inc. ©
2010 。 本講義僅引用部分內容,請讀者自行準備。本作品轉載自 Microsoft Office 2007 多媒體藝廊,依據Microsoft服務合約及著作權法第 46 、 52 、 65 條合理使用。
2
Function: An Overview• Why functions?
o More manageable
o Software use
o Avoid repeated code
• Two types of functionso 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
3
User-Defined Function • Function definition
return_type fnu_name (para_list) {
declaration; statements; return;
} e.g.,
int square( int y ) {
return y * y; }
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 <ctime> # include “my_header.h”
5
User-Defined Function: An Example (1)
1 4 9 16 25 36 49 64 81 100
6
User-Defined Function: An Example (1’)
1 4 9 16 25 36 49 64 81 100
7
User-Defined Function: An Example (2)
8
User-Defined Function: An Example (2)
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;
}
void main() {declarations; statements;}
10
Empty Parameter List
p.239
11
Function: An Overview (cont.)C++ Standard Library header file
Explanation
<iostream>
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 <iostream.h>.
<iomanip>
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 <iomanip.h>.
<cmath>Contains function prototypes for math library functions (discussed in Section 6.3). This header file replaces header file <math.h>.
12
Function: An Overview (cont.)C++ Standard Library header file
Explanation
<cstdlib>
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 <stdlib.h>.
<ctime>Contains function prototypes and types for manipulating the time and date. This header file replaces header file <time.h>. This header file is used in Section 6.7.
<cstring>
Contains function prototypes for C-style string-processing functions. This header file replaces header file <string.h>. This header file is used in Chapter 11, Operator Overloading; String and Array Objects.
13
Function: An Overview (cont.)C++ Standard Library header file
Explanation
<vector>, <list>, <deque>, <queue>, <stack>, <map>, <set>, <bitset>
These header files contain classes that implement the C++ Standard Library containers. Containers store data during a program’s execution. The <vector> header is first introduced in Chapter 7, Arrays and Vectors. We discuss all these header files in Chapter 23, Standard Template Library (STL).
<cctype>
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 <ctype.h>. 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.)C++ Standard Library header file
Explanation
<typeinfo>Contains classes for runtime type identification (determining data types at execution time). This header file is discussed in Section 13.8.
<exception>, <stdexcept>
These header files contain classes that are used for exception handling (discussed in Chapter 16).
<memory>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.
<fstream>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 <fstream.h>.
15
Function: An Overview (cont.)C++ Standard Library header file
Explanation
<string> Contains the definition of class string from the C++ Standard Library (discussed in Chapter 18).
<sstream>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).
<functional>Contains classes and functions used by C++ Standard Library algorithms. This header file is used in Chapter 23.
<iterator>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.)C++ Standard Library header file
Explanation
<algorithm>Contains functions for manipulating data in C++ Standard Library containers. This header file is used in Chapter 23.
<cassert>Contains macros for adding diagnostics that aid program debugging. This replaces header file <assert.h> from pre-standard C++. This header file is used in Appendix F, Preprocessor.
<cfloat>Contains the floating-point size limits of the system. This header file replaces header file <float.h>.
<cstdio>Contains function prototypes for the C-style standard input/output library functions and information used by them. This header file replaces header file <stdio.h>.
17
Function: An Overview (cont.)C++ Standard Library header file
Explanation
<climits> Contains the integral size limits of the system. This header file replaces header file <limits.h>.
<locale>
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.).
<limits> Contains classes for defining the numerical data type limits on each computer platform.
<utility> 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 Formatfunction_name(argument list)
e.g., double x=9.0, y; y=sqrt(x);
(+) write once, use many
function callreturn value y=f(x)
e.g., pow(x,y);
sqrt(9);sqrt(x);sqrt(sqrt(x));
19
Function: An Example (1)
p.221
Note: using namespace std;
20
Function: An Example (2)
p.222-223
21
Function: An Example (3)
p.224-225
22
Function: An Overview (cont.) • Control passing
o Main program → function → Function call
o Function → main program → Return
boss
worker1 worker2 worker3
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
24
Promotion Hierarchy 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
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
27
Storage Class and Scope: An Example
p.233-234
28
Function Call Stack
p.236
29
Function Call Stack (cont.) Step 1: Operating system invokes main to execute application.
______________________________________
Operating System
Return location: R1Automatic variables:
10a
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.) Step 2: main invokes function square to perform calculation. {
int a = 10;cout<<a<<“squared:”<<square(a)<<endl;return 0;
}
int main()
Return location: R1Automatic variables:
10a
Function call stack after Step 2
Activation record for function main
Top of stack Return location: R2
Automatic variables:
10x
Activation record for function square
{return x*x;
}
int square (int x)Return location
R2
31
Function Call Stack (cont.) Step 3: square returns its results to main.
{int a = 10;cout<<a<<“squared:”<<square(a)<<endl;return 0;
}
int main()
Return location: R1Automatic variables:
10a
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
p.240-241
33
Default Argument
p.245-246
版權聲明
34
頁碼 作品 版權圖示 來源 / 作者1-35
本作品轉載自 Microsoft Office 2007 多媒體藝廊,依據 Microsoft 服務合約及著作權法第 46 、 52 、 65 條合理使用。
2Open Clip Art Library ,作者: [email protected] ,本作品轉載自:http://openclipart.org/detail/64771/[email protected],瀏覽日期: 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 。
11-17C++ How to Program, 7/e ,作者: Harvey M. Deitel and Paul J. Deitel ,出版社: Deitel & Associates ,出版日期: 2010 , P.219~220 。依據著作權法第 46 、 52 、 65 條合理使用。
版權聲明
35
頁碼 作品 版權圖示 來源 / 作者
22C++ How to Program, 7/e ,作者: Harvey M. Deitel and Paul J. Deitel ,出版社: Deitel & Associates ,出版日期: 2010 , P.210 。依據著作權法第 46 、 52 、 65 條合理使用。
24C++ How to Program, 7/e ,作者: Harvey M. Deitel and Paul J. Deitel ,出版社: Deitel & Associates ,出版日期: 2010 , P.218 。依據著作權法第 46 、 52 、 65 條合理使用。
26Open Clip Art Library ,作者: Machovka ,本作品轉載自:http://openclipart.org/detail/2297/floppy-diskette-by-machovka,瀏覽日期: 2013/1/21 。
29-30C++ How to Program, 7/e ,作者: Harvey M. Deitel and Paul J. Deitel ,出版社: Deitel & Associates ,出版日期: 2010 , P.237 。依據著作權法第 46 、 52 、 65 條合理使用。
31C++ How to Program, 7/e ,作者: Harvey M. Deitel and Paul J. Deitel ,出版社: Deitel & Associates ,出版日期: 2010 , P.238 。依據著作權法第 46 、 52 、 65 條合理使用。