1 objectives ❏ to design and implement programs with more than one function ❏ to be able to...

70
1 Objectives To design and implement programs with more than one function To be able to design multi-function programs To understand the purpose of the function declaration, call, and definition To understand the four basic function designs To understand how two functions communicate through parameters To understand the differences between global and local scope Chapter 4 Chapter 4 Functions Functions

Upload: felix-moore

Post on 17-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

1

Objectives ❏ To design and implement programs with more than one function❏ To be able to design multi-function programs❏ To understand the purpose of the function declaration, call, and

definition❏ To understand the four basic function designs❏ To understand how two functions communicate through

parameters❏ To understand the differences between global and local scope

Chapter 4Chapter 4 FunctionsFunctions

Page 2: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

2

FIGURE 4-1 Derived Types

Page 3: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

3

4-1 Designing Structured Programs

The programs we have presented so far have been very The programs we have presented so far have been very simple. They solved problems that could be understood simple. They solved problems that could be understood without too much effort. without too much effort.

The principles of top–down design and structured The principles of top–down design and structured programming dictate that a program should be divided programming dictate that a program should be divided into a main module and its related modules. Each into a main module and its related modules. Each module should also be divided into submodules module should also be divided into submodules according to software engineering principles that we according to software engineering principles that we discuss in Section 4.8, “Software Engineering.”discuss in Section 4.8, “Software Engineering.”

Page 4: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

4

In top–down design, a program is divided into a main module and its related modules.

Each module is in turn divided into submodules until the resulting modules are intrinsic; that is, until they are

implicitly understood without further division.

NoteNote

Page 5: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

5

FIGURE 4-2 Structure Chart

Page 6: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

6

4-2 Functions in C

In C, the idea of top–down design is done using In C, the idea of top–down design is done using functions. A C program is made of one or more functions. A C program is made of one or more functions, one and only one of which must be namedfunctions, one and only one of which must be namedmainmain. .

In general, the purpose of a function is to receive zero In general, the purpose of a function is to receive zero or more pieces of data, operate on them, and return at or more pieces of data, operate on them, and return at most one piece of data. At the same time, a function most one piece of data. At the same time, a function can have a side effect. A function side effect is an can have a side effect. A function side effect is an action that results in a change in the state of the action that results in a change in the state of the program. program.

Page 7: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

7

In C, a program is made of one or more functions, one and only one of which must be called main.

The execution of the program always starts with main, but it can call other functions

to do some part of the job.

NoteNote

Page 8: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

8

FIGURE 4-3 Structure Chart for a C Program

Page 9: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

9

FIGURE 4-4 Function Concept

Page 10: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

10

A function in C can have a return value, a side effect, or both.

The side effect occurs before the value is returned. The function’s value is the value in the expression of

the return statement. A function can be calledfor its value, its side effect, or both.

NoteNote

Page 11: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

11

PROGRAM 4-1 Sample Program with Subfunction

Page 12: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

12

Page 13: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

13

4-3 User-Defined Functions

Like every other object in C, functions must be both Like every other object in C, functions must be both declared and defined. The function declaration gives declared and defined. The function declaration gives the whole picture of the function that needs to be the whole picture of the function that needs to be defined later. The function definition contains the defined later. The function definition contains the code for a function.code for a function.

Basic Function DesignsFunction DefinitionFunction DeclarationThe Function Call

Topics discussed in this section:Topics discussed in this section:

Page 14: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

14

A function name is used three times: for declaration, in a call, and for definition.

NoteNote

Page 15: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

15

FIGURE 4-5 Declaring, Calling, and Defining Functions

Defining Function

Page 16: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

16

FIGURE 4-6 void Function with Parameters

Page 17: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

17

PROGRAM 4-2

void Function with a Parameter

Page 18: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

18

FIGURE 4-7 Non-void Function without Parameters

Page 19: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

19

FIGURE 4-8 Calling a Function That Returns a Value

Page 20: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

20

PROGRAM 4-3

Read a Number and Square It

Page 21: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

21

Page 22: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

22

FIGURE 4-9 Function Definition

Page 23: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

23

FIGURE 4-10 Function Return Statements

Page 24: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

24

FIGURE 4-11 Function Local Variables

Page 25: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

25

Formal (形式 ) and Actual (實際 ) ParametersFormal parameters are variables that are declared in the header of the function definition.

Actual parameters are the expressions in the calling statement.

Formal and actual parameters must match exactly in type, order, and number.

Their names, however, do not need to match.

NoteNote

Page 26: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

26

FIGURE 4-12 Parts of a Function Call

Page 27: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

27

FIGURE 4-13 Examples of Function Calls

Page 28: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

28

PROGRAM 4-4

Print Least Significant Digit

Page 29: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

29

FIGURE 4-14 Design for Add Two Digits

Page 30: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

30

PROGRAM 4-5 Add Two Digits

2015/03/18

Page 31: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

31

PROGRAM 4-6

Print Six Digits with Comma

Page 32: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

32

FIGURE 4-15 Design for Strange College fees

Page 33: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

33

PROGRAM 4-7

Strange College Fees

/* This program prints the tuition at Strange College. Strange charges $10 for registration, plus $10 per unit and a penalty of $50 for each 12 units, or fraction of 12, over 12. */

Page 34: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

34

4-4 Inter-Function Communication

Although the calling and called functions are two Although the calling and called functions are two separate entities, they need to communicate to exchange separate entities, they need to communicate to exchange data. The data flow between the calling and called data. The data flow between the calling and called functions can be divided into three strategies: a functions can be divided into three strategies: a downward flow, an upward flow, and a bi-directional downward flow, an upward flow, and a bi-directional flow. flow.

Basic ConceptC Implementation

Topics discussed in this section:Topics discussed in this section:

Page 35: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

35

FIGURE 4-16 Data Flow Strategies

Page 36: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

36

FIGURE 4-17 Downward Communication in C

Page 37: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

37

FIGURE 4-18 Downward Communication

Page 38: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

38

FIGURE 4-19 Upward Communication in C

Call by address

Page 39: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

39

FIGURE 4-20 Upward Communication

Page 40: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

40

To send data from the called function to the calling function:

1. We need to use the & symbol in front of the data variable when we call the function.2. We need to use the * symbol after the data type when we declare the address variable3. We need to use the * in front of the variable when we store data indirect

NoteNote

Page 41: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

41

FIGURE 4-21 Bi-directional Communication in C

Page 42: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

42

FIGURE 4-22 Bi-directional Communication

%

Page 43: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

43

FIGURE 4-23 Exchange Function

Page 44: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

44

FIGURE 4-24 Calculate Quotient and Remainder

Page 45: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

45

FIGURE 4-25 Quotient and Remainder Design

Page 46: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

46

PROGRAM 4-8

Quotient and Remainder

Page 47: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

47

Page 48: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

48

4-5 Standard Functions

C provides a rich collection of standard functions C provides a rich collection of standard functions whose definitions have been written and are ready to whose definitions have been written and are ready to be used in our programs. To use these functions, we be used in our programs. To use these functions, we must include their function declarations.must include their function declarations.

Math FunctionsRandom Numbers

Topics discussed in this section:Topics discussed in this section:

Page 49: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

49

FIGURE 4-26 Library Functions and the Linker

Page 50: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

50

Absolution Value Functions

Page 51: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

51

FIGURE 4-27 Ceiling Function

Page 52: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

52

FIGURE 4-28 Floor Function

Page 53: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

53

Truncation and Round Function

Page 54: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

54

Power and Square Root Function

Page 55: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

55

FIGURE 4-29 Random Number Generation

Random numbers

Page 56: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

56

FIGURE 4-30 Generating a Random Number Series

Page 57: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

57

srand must be called only once for each randomnumber series.

NoteNote

Page 58: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

58

PROGRAM 4-9

Creating Temporal Random Numbers

Page 59: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

59

PROGRAM 4-10

Creating Pseudorandom Numbers

Page 60: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

60

FIGURE 4-31 Random Number Scaling for 3–7

Page 61: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

61

PROGRAM 4-11 Generating Random Numbers in the Range 10 to 20

Page 62: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

62

PROGRAM 4-12

Generating Random Real Numbers

Page 63: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

63

4-6 Scope

Scope determines the region of the program in which a Scope determines the region of the program in which a defined object is visible. Scope pertains to any object defined object is visible. Scope pertains to any object that can be declared, such as a variable orthat can be declared, such as a variable ora function declaration. a function declaration.

Global ScopeLocal Scope

Topics discussed in this section:Topics discussed in this section:

Page 64: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

64

FIGURE 4-32 Scope for Global and Block Areas

Page 65: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

65

Variables are in scope from declaration until the end of their block.

NoteNote

Page 66: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

66

4-7 Programming Example— Incremental Development

Top–down development, a concept inherent to Top–down development, a concept inherent to modular programming, allows us to develop programs modular programming, allows us to develop programs incrementally. By writing and debugging each incrementally. By writing and debugging each function separately, we are able to solve the program function separately, we are able to solve the program in smaller steps, making the whole process easier.in smaller steps, making the whole process easier.

First Increment: main and getDataSecond Increment: addFinal Increment: Print ResultsThe

Topics discussed in this section:Topics discussed in this section:

Page 67: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

67

FIGURE 4-33 Calculator Program Design

Page 68: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

68

PROGRAM 4-13

Calculator Program—First Increment

Page 69: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

69

PROGRAM 4-14 Calculator Program—Second Increment

Page 70: 1 Objectives ❏ To design and implement programs with more than one function ❏ To be able to design multi-function programs ❏ To understand the purpose

70

PROGRAM 4-15 Calculator Program—Final Increment