35
計計計計計 第第第第 Function I 第第第第 第第第第第 計計 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 第第第第第

計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

Embed Size (px)

Citation preview

Page 1: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

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 條合理使用。

Page 2: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

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

Page 3: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

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.

Page 4: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

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”

Page 5: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

5

User-Defined Function: An Example (1)

1 4 9 16 25 36 49 64 81 100

Page 6: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

6

User-Defined Function: An Example (1’)

1 4 9 16 25 36 49 64 81 100

Page 7: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

7

User-Defined Function: An Example (2)

Page 8: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

8

User-Defined Function: An Example (2)

Page 9: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

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;}

Page 10: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

10

Empty Parameter List

p.239

Page 11: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

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>.

Page 12: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

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.

Page 13: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

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.

Page 14: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

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>.

Page 15: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

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).

Page 16: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

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>.

Page 17: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

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.

Page 18: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

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));

Page 19: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

19

Function: An Example (1)

p.221

Note: using namespace std;

Page 20: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

20

Function: An Example (2)

p.222-223

Page 21: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

21

Function: An Example (3)

p.224-225

Page 22: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

22

Function: An Overview (cont.) • Control passing

o Main program → function → Function call

o Function → main program → Return

boss

worker1 worker2 worker3

worker4worker5

Page 23: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

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

Page 24: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

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

Page 25: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

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

Page 26: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

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

Page 27: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

27

Storage Class and Scope: An Example

p.233-234

Page 28: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

28

Function Call Stack

p.236

Page 29: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

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()

Page 30: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

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

Page 31: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

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

Page 32: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

32

Inline Function

p.240-241

Page 33: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

33

Default Argument

p.245-246

Page 34: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

版權聲明

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 條合理使用。

Page 35: 計算機程式 第四單元 Function I 授課教師:廖婉君教授 【本著作除另有註明外,採取創用 CC 「姓名標示 -非商業性-相同方式分享」台灣

版權聲明

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 條合理使用。