CHAPTER 7 USER-DEFINED FUNCTIONS II - PowerPoint PPT Presentation

1 / 118
About This Presentation
Title:

CHAPTER 7 USER-DEFINED FUNCTIONS II

Description:

... it is typically used to exit the function early. ... Spring Sale ... lines; for(lines ... – PowerPoint PPT presentation

Number of Views:134
Avg rating:3.0/5.0
Slides: 119
Provided by: dmalik
Category:

less

Transcript and Presenter's Notes

Title: CHAPTER 7 USER-DEFINED FUNCTIONS II


1
CHAPTER 7USER-DEFINED FUNCTIONS II
2
  • In this chapter, you will
  • Learn how to construct and use void functions in
    a program
  • Discover the difference between value and
    reference parameters
  • Explore reference parameters and value-returning
    functions
  • Learn about the scope of an identifier
  • Examine the difference between local and global
    identifiers
  • Discover static variables
  • Learn function overloading
  • Explore functions with default parameters

3
  • VOID FUNCTIONS
  • Void functions and value-returning functions have
    similar structures.
  • Both have a heading part and a statement part.
  • User-defined void functions can be placed either
    before or after the function main.
  • The program execution always begins with the
    first statement in the function main.
  • If you place user-defined void functions after
    the function main, you should place the function
    prototype before the function main.
  • A void function does not have a data type and so
    functionType in the heading part and the return
    statement in the body of the void function are
    meaningless.
  • In a void function, you can use the return
    statement without any value it is typically used
    to exit the function early.
  • Void functions may or may not have formal
    parameters.
  • A call to a void function is a stand-alone
    statement.

4
  • Void Functions without Parameters
  • Syntax Function Definition
  • void functionName(void)
  • statements
  • The word void inside the parentheses is optional.
  • In C, void is a reserved word.
  • Syntax Function Call
  • functionName()

5
  • Example 7-1
  • Annual
  • Spring Sale

6
  • include ltiostreamgt
  • using namespace std
  • void printStars(void)
  • int main()
  • printStars() //Line 1
  • coutltlt" Annual "ltltendl
    //Line 2
  • printStars() //Line 3
  • coutltlt" Spring Sale "ltltendl
    //Line 4
  • printStars() //Line 5
  • return 0
  • void printStars(void)
  • coutltlt""ltltendl
  • coutltlt""ltltendl

7
  • The statement printStars() in the function main
    is a function call.
  • You can replace the function printStars by the
    following function.
  • void printStars(void)
  • int stars, lines
  • for(lines 1 lines lt 2 lines) //Line
    1
  • for(stars 1 stars lt 30 stars) //Line
    2
  • coutltlt"" //Line 3
  • coutltltendl //Line 4

8
  • Write a program to print the following pattern.

9
  • Void Functions with Parameters
  • Syntax Function Definition
  • void functionName(formal parameter list)
  • statements
  • Syntax Formal parameter list
  • dataType variable, dataType variable, ...

10
  • Syntax Function Call
  • functionName(actual parameter list)
  • Syntax Actual Parameter List
  • expression or variable, expression or variable,

11
  • Example 7-2
  • void funexp(int a, double b, char c, int x)
  • .
  • .
  • .
  • Parameters provide a communication link between
    the calling function (such as main) and the
    called function. They enable functions to
    manipulate different data each time they are
    called.

12
  • Value Parameter A formal parameter that receives
    a copy of the content of the corresponding actual
    parameter.
  • Reference Parameter A formal parameter that
    receives the location (memory address) of the
    corresponding actual parameter.
  • When we attach after the dataType in the formal
    parameter list of a function, then the variable
    following that dataType becomes a reference
    parameter.
  • Example 7-3
  • void expfun(int one, int two, char three,
    double four)

13
  • If a formal parameter is a value parameter, the
    value of the corresponding actual parameter is
    copied into the formal parameter.
  • The value parameter has its own copy of the data.
  • During program execution, the value parameter
    manipulates the data stored in its own memory
    space.
  • If a formal parameter is a reference parameter,
    it receives the address of the corresponding
    actual parameter.
  • A reference parameter stores the address of the
    corresponding actual parameter.
  • During program execution to manipulate the data,
    the address stored in the reference parameter
    directs it to the memory space of the
    corresponding actual parameter.

14
  • Example 7-4
  • //Program Print a triangle of stars
  • include ltiostreamgt
  • using namespace std
  • void printStars(int blanks, int starsInLine)
  • int main()
  • int numberOfLines
  • int counter
  • int numberOfBlanks
  • coutltlt"Enter the number of star lines (1 to
    20)"
  • ltlt" to be printed--gt " //Line 1
  • cingtgtnumberOfLines //Line 2

15
  • while(numberOfLines lt 0 numberOfLines gt 20)
    //Line 3
  • coutltlt"Number of star lines should be "
  • ltlt"between 1 and 20"ltltendl //Line 4
  • coutltlt"Enter the number of star lines "
  • ltlt"(1 to 20)to be printed--gt " //Line 5
  • cingtgtnumberOfLines //Line 6
  • coutltltendlltltendl //Line 7
  • numberOfBlanks 30 //Line 8
  • for(counter 1 counter lt numberOfLines
  • counter) //Line 9
  • printStars(numberOfBlanks, counter) //Line 10
  • numberOfBlanks-- //Line 11

16
  • return 0 //Line 12
  • void printStars(int blanks, int starsInLine)
  • int count
  • for(count 1 count lt blanks count) //Line
    13
  • coutltlt" " //Line 14
  • for(count 1 count lt starsInLine count)
    //Line 15
  • coutltlt" " //Line 16
  • coutltltendl

17
  • Sample Run The user input is in red.
  • Enter the number of star lines (1 to 20) to be
    printed--gt 15


18
  • Reference Parameters
  • Value parameters provide only a one-way link
    between actual parameters and formal parameters.
  • A reference parameter receives the address of the
    actual parameter.
  • Reference parameters can pass one or more values
    from a function and can change the value of the
    actual parameter.
  • Reference parameters are useful in three
    situations
  • When you want to return more than one value from
    a function
  • When the value of the actual parameter needs to
    be changed
  • When passing the address would save memory space
    and time relative to copying a large amount of
    data

19
  • Example 7-5
  • Given the course score (a value between 0 and
    100), the following program determines the course
    grade of a student.
  • 1. main
  • a. Get Course Score.
  • b. Print Course Grade.
  • 2. getScore
  • a. Prompt the user for the input.
  • b. Get the input.
  • c. Print course score.
  • 3. printGrade
  • a. Calculate the course grade.
  • b. Print course grade.

20
  • // Program Compute grade.
  • // This program reads a course score and prints
    the
  • // associated course grade.
  • include ltiostreamgt
  • using namespace std
  • void getScore(int score)
  • void printGrade(int score)
  • int main ()
  • int courseScore
  • coutltlt"Line 1 Based on the course score, this
    program "
  • ltlt"computes the course grade."ltltendl //Line
    1

21
  • getScore(courseScore) //Line 2
  • printGrade(courseScore) //Line 3
  • return 0
  • void getScore(int score)
  • coutltlt"Line 4 Enter course score--gt " //Line
    4
  • cingtgtscore //Line 5
  • coutltltendlltlt"Line 6 Course score is "
  • ltltscoreltltendl //Line 6

22
  • void printGrade(int score)
  • coutltlt"Line 7 Your grade for the course is
    " //Line 7
  • if(score gt 90) //Line 8
  • coutltlt"A"ltltendl
  • else if(score gt 80)
  • coutltlt"B"ltltendl
  • else if(score gt 70)
  • coutltlt"C"ltltendl
  • else if(score gt 60)
  • coutltlt"D"ltltendl
  • else
  • coutltlt"F"ltltendl

23
  • Sample Run
  • Line 1 Based on the course score, this program
    computes the course grade.
  • Line 4 Enter course score--gt 85
  • Line 6 Course score is 85
  • Line 7 Your grade for the course is B

24
  • VALUE AND REFERENCE PARAMETERS AND MEMORY
    ALLOCATION
  • When a function is called, memory for its formal
    parameters and variables declared in the body of
    the function (called local variables) is
    allocated in the function data area.
  • In the case of a value parameter, the value of
    the actual parameter is copied into the memory
    cell of its corresponding formal parameter.
  • In the case of a reference parameter, the address
    of the actual parameter passes to the formal
    parameter.
  • Content of the formal parameter is an address.
  • During execution, changes made by the formal
    parameter permanently change the value of the
    actual parameter.
  • Stream variables (for example, ifstream and
    ofstream) should be passed by reference to a
    function.

25
  • Example 7-6
  • //Example 7-6 Reference and value parameters
  • include ltiostreamgt
  • using namespace std
  • void funOne(int a, int b, char v)
  • void funTwo(int x, int y, char w)
  • int main()
  • int num1, num2
  • char ch
  • num1 10 //Line 1
  • num2 15 //Line 2
  • ch 'A' //Line 3

26
  • coutltlt"Line 4 Inside main num1 "ltltnum1
  • ltlt", num2 "ltltnum2ltlt", and ch
    "ltltchltltendl //Line 4
  • funOne(num1, num2, ch) //Line 5
  • coutltlt"Line 6 After funOne num1 "ltltnum1
  • ltlt", num2 "ltltnum2ltlt", and ch
    "ltltchltltendl //Line 6
  • funTwo(num2, 25, ch) //Line 7
  • coutltlt"Line 8 After funTwo num1 "ltltnum1
  • ltlt", num2 "ltltnum2ltlt", and ch "ltltchltltendl
    //Line
  • return 0

27
  • void funOne(int a, int b, char v)
  • int one
  • one a //Line 9
  • a //Line 10
  • b b 2 //Line 11
  • v 'B' //Line 12
  • coutltlt"Line 13 Inside funOne a "ltltaltlt", b
    "ltltb
  • ltlt", v "ltltvltlt", and one
    "ltltoneltltendl//Line 13
  • void funTwo(int x, int y, char w)
  • x //Line 14
  • y y 2 //Line 15
  • w 'G' //Line 16
  • coutltlt"Line 17 Inside funTwo x "ltltx

28
  • Output
  • Line 4 Inside main num1 10, num2 15, and ch
    A
  • Line 13 Inside funOne a 11, b 30, v B,
    and one 10
  • Line 6 After funOne num1 10, num2 30, and
    ch A
  • Line 17 Inside funTwo x 31, y 50, and w G
  • Line 8 After funTwo num1 10, num2 31, and
    ch G

29
  • Just before Line 1 executes, memory is allocated
    only for the variables of the function main this
    memory is not initialized. After Line 3 executes,
    the variables are as shown in Figure 7-1.

30
  • coutltlt"Line 4 Inside main num1 "ltltnum1
  • ltlt", num2 "ltltnum2ltlt", and ch
    "ltltchltltendl //Line 4
  • Line 4 produces the following output.
  • Line 4 Inside main num1 10, num2 15, and ch
    A

31
  • one a //Line 9
  • Just before Line 9 executes, the variables are as
    shown in Figure 7-2.

32
  • one a //Line 9
  • After Line 9 (one a)executes, the variables
    are as shown in Figure 7-3.

33
  • a //Line 10
  • After Line 10 (a) executes, the variables are
    as shown in Figure 7-4.

34
  • b b 2 //Line 11
  • After Line 11 (b b 2) executes, the
    variables are as shown in Figure 7-5.

35
  • v 'B' //Line 12
  • After Line 12 (v 'B') executes, the variables
    are as shown in Figure 7-6.

36
  • coutltlt"Line 13 Inside funOne a "ltltaltlt", b
    "ltltb
  • ltlt", v "ltltvltlt", and one
    "ltltoneltltendl//Line 13
  • Line 13 produces the following output.
  • Line 13 Inside funOne a 11, b 30, v B,
    and one 10

37
  • After the execution of line 13, control goes back
    to line 6 and memory allocated for the variables
    of function funOne is deallocated. Figure 7-7
    shows the values of the variables of the function
    main.

38
  • coutltlt"Line 6 After funOne num1 "ltltnum1
  • ltlt", num2 "ltltnum2ltlt", and ch
    "ltltchltltendl //Line 6
  • Line 6 produces the following output.
  • Line 6 After funOne num1 10, num2 30, and
    ch A

39
  • x //Line 14
  • Before the execution of Line 14.

40
  • x //Line 14
  • After Line 14 (x) executes, the variables are
    as shown in Figure 7-9.

41
  • y y 2 //Line 15
  • After Line 15 (y y 2)executes, the variables
    are as shown in Figure 7-10.

42
  • w 'G' //Line 16
  • After Line 16 (w 'G') executes, the variables
    are as shown in Figure 7-11.

43
  • coutltlt"Line 17 Inside funTwo x "ltltx
  • ltlt", y "ltlty ltlt", and w
    "ltltwltltendl//Line 17
  • Line 17 produces the following output.
  • Line 17 Inside funTwo x 31, y 50, and w
    G

44
  • After the execution of Line 17, control goes to
    Line 8. The memory allocated for the variables of
    function funTwo is deallocated.

45
  • coutltlt"Line 8 After funTwo num1 "ltltnum1
  • ltlt", num2 "ltltnum2ltlt", and ch "ltltchltltendl
    //Line 8
  • Line 8 produces the following output.
  • Line 8 After funTwo num1 10, num2 31, and
    ch G
  • After the execution of line 8, the program
    terminates.

46
  • Example 7-7
  • //Example 7-7 Reference and value parameters
  • //Program Makes You Think.
  • include ltiostreamgt
  • using namespace std
  • void addFirst(int first, int second)
  • void doubleFirst(int one, int two)
  • void squareFirst(int ref, int val)

47
  • int main ()
  • int num 5
  • coutltlt"Line 1 Inside main num
    "ltltnumltltendl //Line 1
  • addFirst(num,num) //Line 2
  • coutltlt"Line 3 Inside main after addFirst"
  • ltlt" num " ltltnumltltendl //Line 3
  • doubleFirst(num,num) //Line 4
  • coutltlt"Line 5 Inside main after "
  • ltlt"doubleFirst num "ltltnumltltendl //Line
    5
  • squareFirst(num,num) //Line 6
  • coutltlt"Line 7 Inside main after "
  • ltlt"squareFirst num "ltltnumltltendl//Line
    7
  • return 0

48
  • void addFirst(int first, int second)
  • coutltlt"Line 8 Inside addFirst first
    "ltltfirst
  • ltlt", second "ltltsecondltltendl //Line 8
  • first first 2 //Line 9
  • coutltlt"Line 10 Inside addFirst first
    "ltltfirst
  • ltlt", second "ltltsecondltltendl //Line 10
  • second second 2 //Line 11
  • coutltlt"Line 12 Inside addFirst first
    "ltltfirst
  • ltlt", second "ltltsecondltltendl //Line 12

49
  • void doubleFirst(int one, int two)
  • coutltlt"Line 13 Inside doubleFirst one
    "ltltone
  • ltlt", two "ltlttwoltltendl //Line 13
  • one one 2 //Line 14
  • coutltlt"Line 15 Inside doubleFirst one
    "ltltone
  • ltlt", two "ltlttwoltltendl //Line 15
  • two two 2 //Line 16
  • coutltlt"Line 17 Inside doubleFirst one
    "ltltone
  • ltlt", two "ltlttwoltltendl //Line 17

50
  • void squareFirst(int ref, int val)
  • coutltlt"Line 18 Inside squareFirst ref "
  • ltltref ltlt", val "ltlt valltltendl //Line 18
  • ref ref ref //Line 19
  • coutltlt"Line 20 Inside squareFirst ref "
  • ltltref ltlt", val "ltlt valltltendl //Line 20
  • val val 2 //Line 21
  • coutltlt"Line 22 Inside squareFirst ref "
  • ltltref ltlt", val "ltlt valltltendl //Line 22

51
  • Output
  • Line 1 Inside main num 5
  • Line 8 Inside addFirst first 5, second 5
  • Line 10 Inside addFirst first 7, second 7
  • Line 12 Inside addFirst first 14, second
    14
  • Line 3 Inside main after addFirst num 14
  • Line 13 Inside doubleFirst one 14, two 14
  • Line 15 Inside doubleFirst one 28, two 14
  • Line 17 Inside doubleFirst one 28, two 16
  • Line 5 Inside main after doubleFirst num 14
  • Line 18 Inside squareFirst ref 14, val 14
  • Line 20 Inside squareFirst ref 196, val 14
  • Line 22 Inside squareFirst ref 196, val 16
  • Line 7 Inside main after squareFirst num 196

52
addFirst(num,num) //Line 2
53
  • doubleFirst(num,num) //Line 4

54
  • squareFirst(num,num) //Line 6

55
  • Example 7-8
  • //Example 7-8 Reference and value parameters
  • include ltiostreamgt
  • using namespace std
  • int t
  • void funOne(int a,int x)
  • void funTwo(int u,int v)
  • int main()
  • int num1, num2
  • num1 10 //Line 1
  • num2 20 //Line 2
  • t 15 //Line 3

56
  • coutltlt"Line 4 In main num1 "ltltnum1ltlt",
    num2 "
  • ltltnum2ltlt", and t "ltltt ltltendl //Line 4
  • funOne(num1, t) //Line 5
  • coutltlt"Line 6 In main after funOne "ltlt"num1
    "
  • ltltnum1ltlt", num2 "ltltnum2ltlt", and t "ltltt
  • ltltendl //Line 6
  • funTwo(num2, num1) //Line 7
  • coutltlt"Line 8 In main after funTwo "ltlt"num1
    "
  • ltltnum1ltlt", num2 "ltltnum2ltlt", and t "
  • ltlttltltendl //Line 8
  • return 0 //Line 9

57
  • void funOne(int a, int x)
  • int z
  • z a x //Line 10
  • coutltlt"Line 11 In funOne a "ltltaltlt", x
    "ltltx
  • ltlt", z "ltltzltlt", and t "ltlttltltendl //Line 11
  • x x 5 //Line 12
  • coutltlt"Line 13 In funOne a "ltltaltlt", x
    "ltltx
  • ltlt", z "ltltzltlt", and t
    "ltlttltltendl//Line 13
  • a a 12 //Line 14
  • coutltlt"Line 15 In funOne a "ltltaltlt", x
    "ltltx
  • ltlt", z "ltltzltlt", and t
    "ltlttltltendl//Line 15
  • t t 13 //Line 16

58
  • void funTwo(int u, int v)
  • int aTwo
  • aTwo u //Line 18
  • coutltlt"Line 19 In funTwo u "ltltultlt", v
    "ltltv
  • ltlt", aTwo "ltltaTwoltlt", and t "ltlttltltendl
    //Line 19
  • funOne(aTwo,u) //Line 20
  • coutltlt"Line 21 In funTwo after funOne"ltlt " u
    "
  • ltltultlt", v "ltltvltlt", aTwo "ltltaTwo
  • ltlt", and t "ltlttltltendl //Line 21
  • u u 13 //Line 22
  • coutltlt"Line 23 In funTwo u "ltltultlt", v
    "ltltv
  • ltlt", aTwo "ltltaTwoltlt", and t "ltlttltltendl
    //Line 23
  • t 2 t //Line 24
  • coutltlt"Line 25 In funTwo u "ltltultlt", v
    "ltltv

59
  • OutPut
  • Line 4 In main num1 10, num2 20, and t 15
  • Line 11 In funOne a 10, x 15, z 25, and t
    15
  • Line 13 In funOne a 10, x 20, z 25, and t
    20
  • Line 15 In funOne a 22, x 20, z 25, and t
    20
  • Line 17 In funOne a 22, x 33, z 25, and t
    33
  • Line 6 In main after funOne num1 22, num2
    20, and t 33
  • Line 19 In funTwo u 20, v 22, aTwo 20,
    and t 33
  • Line 11 In funOne a 20, x 20, z 40, and t
    33
  • Line 13 In funOne a 20, x 25, z 40, and t
    33
  • Line 15 In funOne a 32, x 25, z 40, and t
    33
  • Line 17 In funOne a 32, x 25, z 40, and t
    46
  • Line 21 In funTwo after funOne u 25, v 22,
    aTwo 32, and t 46
  • Line 23 In funTwo u 38, v 22, aTwo 32,
    and t 46
  • Line 25 In funTwo u 38, v 22, aTwo 32,
    and t 92
  • Line 8 In main after funTwo num1 22, num2
    38, and t 92

60
  • REFERENCE PARAMETERS AND VALUE-RETURNING
    FUNCTIONS
  • SCOPE OF AN IDENTIFIER
  • The scope of an identifier refers to where in the
    program an identifier is accessible (that is
    visible).
  • Local identifier Identifiers declared within a
    function (or block).
  • Global identifier Identifiers declared outside
    of every function definition.
  • C does not allow the nesting of functions. That
    is, the definition of one function cannot be
    included in the body of another function.

61
  • 1. Global identifiers (such as variables) are
    accessible by a function or a block if,
  • a. The identifier is declared before the function
    definition (block),
  • b. The function name is different from the
    identifier,
  • c. All parameters of the function have names
    different than the name of the identifier,
  • d. All local identifiers (such as local
    variables) have names different than the name of
    the identifier.
  • 2. (Nested Block) An identifier declared within a
    block is accessible
  • a. Only within the block from the point they are
    declared until the end of the block and
  • b. By those blocks that are nested within that
    block if the nested block does not have an
    identifier with the same name as that of the
    outside block (the block that encloses the nested
    block.)
  • 3. The scope of a function name is similar to the
    scope of an identifier declared outside of any
    block.

62
  • C allows the programmer to declare a variable
    in the initialization statement of the for
    statement.
  • for(int count 1 count lt 10 count)
  • coutltltcountltltendl
  • The scope of the variable count is limited to
    only the body of the for loop.

63
  • include ltiostreamgt
  • using namespace std
  • const double rate 10.50
  • int z
  • double t
  • void one(int x, char y)
  • void two(int a, int b, char x)
  • void three(int one, double y, int z)
  • int main ()
  • int num, first
  • double x, y, z
  • char name, last
  • .
  • .
  • .

64
  • void one(int x, char y)
  • .
  • .
  • .
  • int w
  • void two(int a, int b, char x)
  • int count
  • .
  • .
  • .

65
  • void three(int one, double y, int z)
  • char ch
  • int a
  • .
  • .
  • .
  • //Block four
  • int x
  • char a
  • .
  • .
  • //end Block four
  • .
  • .
  • .

66
(No Transcript)
67
  • 1. Some compilers initialize global variables to
    their default values.
  • 2. In C, is called the scope resolution
    operator. By using the scope resolution operator,
    a global variable declared before the definition
    of a function (block) can be accessed by the
    function (or block) even if the function (or
    block) has an identifier with the same name as
    the variable.
  • 3. C provides a way to access a global variable
    declared after the definition of a function. In
    this case, the function must not contain any
    identifier with the same name as the global
    variable. In the preceding example, the function
    one does not contain any identifier named w.
    Therefore, w can be accessed by function one if
    you declare w as an external variable inside one.
    To declare w as an external variable inside
    function one, the function one must contain the
    following statement
  • extern int w

68
  • SIDE EFFECTS OF GLOBAL VARIABLES
  • Using global variables has side effects.
  • Any function that uses global variables is not
    independent and usually it cannot be used in more
    than one program.
  • Also, if more than one function uses the same
    global variable and if something goes wrong, it
    is difficult to find what went wrong and where.
  • Problems caused by global variables in one area
    of a program might be misunderstood as problems
    caused in another area.
  • Use appropriate parameters instead of using
    global variables.

69
  • STATIC AND AUTOMATIC VARIABLES
  • A variable for which memory is allocated at block
    entry and deallocated at block exit is called an
    automatic variable.
  • A variable for which memory remains allocated as
    long as the program executes is called a static
    variable.
  • Variables declared outside of any block are
    static variables and by default variables
    declared within a block are automatic variables.
  • We can declare a static variable within a block
    by using the reserved word static.
  • The syntax for declaring a static variable is
  • static dataType identifier
  • The statement
  • static int x
  • declares x to be a static variable of the type
    int.
  • Static variables declared within a block are
    local to the block and their scope is the same as
    any other local identifier of that block

70
  • Example 7-9
  • //Program Static and automatic variables
  • include ltiostreamgt
  • using namespace std
  • void test()
  • int main()
  • int count
  • for(count 1 count lt 5 count)
  • test()
  • return 0

71
  • void test()
  • static int x 0
  • int y 10
  • x x 2
  • y y 1
  • coutltlt"Inside test x "ltltxltlt" and y "
  • ltlty ltltendl
  • Output
  • Inside test x 2 and y 11
  • Inside test x 4 and y 11
  • Inside test x 6 and y 11
  • Inside test x 8 and y 11
  • Inside test x 10 and y 11

72
  • FUNCTION OVERLOADING AN INTRODUCTION
  • In C, you can have several functions with the
    same name.
  • If you have several functions with the same name,
    every function must have a different set of
    parameters.
  • In C terminology, this is referred as
    overloading a function name.
  • The types of parameters determine which function
    to be executed.
  • int largerInt(int x, int y)
  • char largerChar(char first, char second)
  • double largerDouble(double u, double v)
  • string largerString(string first, string second)

73
  • Instead of giving different names to these
    functions, we can use the same name, say larger
    for each of the functions, that is, we can
    overload the function larger.
  • int larger(int x, int y)
  • char larger(char first, char second)
  • double larger(double u, double v)
  • string larger(string first, string second)
  • If the call is larger(5,3), the first function
    will be executed.
  • If the call is larger('A', '9'), the second
    function will be executed.
  • Function overloading is used when we have the
    same action for different sets of data.
  • For function overloading to work, we must give
    the definition of each of the functions.

74
  • FUNCTIONS WITH DEFAULT PARAMETERS
  • When a function is called, the number of actual
    and formal parameters must be the same.
  • C relaxes this condition for functions with
    default parameters.
  • You specify the value of a default parameter when
    the function name appears for the first time,
    such as in the prototype.

75
  • In general, the following rules apply for
    functions with default parameters
  • If you do not specify the value of a default
    parameter, the default value is used for that
    parameter.
  • All of the default parameters must be the
    rightmost parameters of the function.
  • Suppose a function has more than one default
    parameter. In a function call, if a value to a
    default parameter is not specified, then you must
    omit all of the arguments to its right.
  • Default values can be constants, global
    variables, or function calls.
  • The caller has the option of specifying a value
    other than the default for any default parameter.
  • You cannot assign a constant value as a default
    value to a reference parameter.

76
  • void funcExp(int x, int y, double t, char z
    'A',
  • int u 67, char v 'G',
  • double w 78.34)
  • int a, b
  • char ch
  • double d
  • The following function calls are legal
  • 1. funcExp(a, b, d)
  • 2. funcExp(a, 15, 34.6,'B', 87, ch)
  • 3. funcExp(b, a, 14.56,'D')
  • The following function calls are illegal
  • 1. funcExp(a, 15, 34.6, 46.7)
  • 2. funcExp(b, 25, 48.76, 'D', 4567, 78.34)

77
  • The following are illegal function prototypes
    with default parameters.
  • void funcOne(int x, double z 23.45, char ch,
  • int u 45)
  • int funcTwo(int length 1, int width,
  • int height 1)
  • void funcThree(int x, int y 16, double z
    34)

78
  • PROGRAMMING EXAMPLE CLASSIFY NUMBERS
  • In this example, using functions we rewrite the
    program that determines the number of odds and
    evens from a given list of integers. This program
    was first written in Chapter 5.
  • The main algorithm remains the same
  • 1. Initialize variables, zeros, odds, and evens
    to 0.
  • 2. Read a number.
  • 3. If the number is even, increment the even
    count, and if the number is also zero, increment
    the zero count else increment the odd count.
  • 4. Repeat Steps 2 and 3 for each number in the
    list.

79
  • To simplify the function main and further
    illustrate parameter passing, the program
    includes
  • A function, initialize, to initialize the
    variables, such as zeros, odds, and evens.
  • A function, getNumber, to get the number.
  • A function, classifyNumber, to determine whether
    the number is odd or even (and whether it is also
    zero). This function also increments the
    appropriate count.
  • A function, printResults, to print the results.

80
  • initialize
  • void initialize(int zeroCount, int oddCount,
  • int evenCount)
  • zeroCount 0
  • oddCount 0
  • evenCount 0
  • getNumber
  • void getNumber(int num)
  • cingtgtnum

81
  • classifyNumber
  • void classifyNumber(int num, int zeroCount,
  • int oddCount, int evenCount)
  • switch(num 2)
  • case 0 evenCount // update even count
  • if(num 0) // number is also zero
  • zeroCount // update zero count
  • break
  • case 1
  • case -1 oddCount // update odd count
  • //end switch

82
  • printResults
  • void printResults(int zeroCount, int oddCount,
  • int evenCount)
  • coutltlt"There are "ltltevenCountltlt" evens, "
  • ltlt"which also includes "ltltzeroCount
  • ltlt" zeros"ltltendl
  • coutltlt"Total number of odds are "
  • ltltoddCountltltendl

83
  • Main Algorithm
  • 1. Call function initialize to initialize
    variables.
  • 2. Prompt the user to enter 20 numbers.
  • 3. For each number in the list
  • a. Call function getNumber to read a number
  • b. Output the number.
  • c. Call function classifyNumber to classify the
    number and increment the appropriate count.
  • 4. Call function printResults to print the final
    results.

84
  • //Program Classify Numbers
  • //This program counts the number of zeros, odd,
    and even
  • //numbers
  • include ltiostreamgt
  • include ltiomanipgt
  • using namespace std
  • const int N 20
  • //function prototypes
  • void initialize(int zeroCount, int oddCount,
  • int evenCount)
  • void getNumber(int num)
  • void classifyNumber(int num, int zeroCount,
  • int oddCount, int
    evenCount)
  • void printResults(int zeroCount, int oddCount,
  • int evenCount)

85
  • int main ()
  • //variable declaration
  • int counter //Loop control variable
  • int number //Store the new number
  • int zeros //Store the number of zeros
  • int odds //Store the number of odd
    integers
  • int evens //Store the number of even
    integers
  • initialize(zeros, odds, evens) //Step 1
  • coutltlt"Please enter "ltltNltlt" integers."
  • ltltendl //Step 2
  • coutltlt"The numbers you entered are--gt "ltltendl

86
  • for (counter 1 counter lt N
    counter) //Step 3
  • getNumber(number) //Step 3a
  • coutltltsetw(3)ltltnumber //Step 3b
  • classifyNumber(number,zeros,odds,evens)//Step
    3c
  • // end for loop
  • coutltltendl
  • printResults(zeros,odds,evens) //Step 4
  • return 0

87
  • void initialize(int zeroCount, int oddCount,
  • int evenCount)
  • zeroCount 0
  • oddCount 0
  • evenCount 0
  • void getNumber(int num)
  • cingtgtnum

88
  • void classifyNumber(int num, int zeroCount,
  • int oddCount, int evenCount)
  • switch(num 2)
  • case 0 evenCount // update even count
  • if(num 0) // number is also zero
  • zeroCount // update zero count
  • break
  • case 1
  • case -1 oddCount // update odd count
  • //end switch

89
  • void printResults(int zeroCount, int oddCount,
  • int evenCount)
  • coutltlt"There are "ltltevenCountltlt" evens, "
  • ltlt"which also includes "ltltzeroCount
  • ltlt" zeros"ltltendl
  • coutltlt"Total number of odds are "
  • ltltoddCountltltendl
  • Sample Run
  • Please enter 20 integers.
  • The numbers you entered are--gt
  • 0 0 12 23 45 7 -2 -8 -3 -9 4 0 1 0 -7 23 -24 0 0
    12
  • 0 0 12 23 45 7 -2 -8 -3 -9 4 0 1 0 -7 23
    -24 0 0 12
  • There are 12 evens, which also includes 6 zeros
  • Total number of odds are 8

90
  • PROGRAMMING EXAMPLE DATA COMPARISON
  • This programming example illustrates
  • How to read data from more than one file in the
    same program
  • How to send output to a file
  • How to generate bar graphs
  • With the help of functions and parameter passing,
    how to use the same program segment on different
    (but similar) sets of data
  • How to use structured design to solve a problem
    and how to perform parameter passing
  • We break the program in two parts. First we
    illustrate how to read data from more than one
    file and then we will show how to generate bar
    graphs.

91
  • Two groups of students at a local university are
    enrolled in certain special courses during the
    summer semester.
  • The courses are offered for the first time and
    are taught by different teachers.
  • At the end of the semester, both groups are given
    the same tests for the same courses and their
    scores are recorded in separate files.
  • The data in each file is in the following form
  • courseNo score1, score2, ..., scoreN 999
  • courseNo score1, score2, ..., scoreM 999
  • ...
  • The output is of the form
  • Course Group Course Average
  • A 1 80.50
  • 2 82.75
  • B 1 78.00
  • 2 75.35

92
  • Input Because data for the two groups are
    recorded in separate files, input data appear in
    two separate files.
  • Output As shown above.

93
  • Problem Analysis and Algorithm Design
  • Reading input data from both files is
    straightforward.
  • Suppose the data are stored in the file
    agroup1.txt for group 1 and in the file
    agroup2.txt for group 2.
  • After processing the data for one group, we can
    process data for the second group for the same
    course, and continue until we run out of data.
  • Processing data for each course is similar and is
    a two-step process
  • 1. a. Sum the score for the course.
  • b. Count the number of students in the
    course
  • c. Divide the total score by the number of
    students to find the course average
  • 2. Output the results.

94
  • The program consists of two functions
  • A function, calculateAverage, to find the course
    average.
  • A function, printResult, to output the data in
    the form given.
  • By passing appropriate parameters, we can use the
    same functions, calculateAverage and printResult,
    to process each courses data for both groups.
  • In the second part of the program only the
    function printResult will be modified.

95
  • 1. Initialize variables.
  • 2. Get course IDs for group 1 and group 2.
  • 3. If the course IDs are different, print an
    error message and exit the program.
  • 4. Calculate the course average for group1 and
    group 2.
  • 5. Print the results in the form given above.
  • 6. Repeat steps 2-6 for each course.
  • 7. Print final results.

96
  • Variables (function main)
  • char courseId1 //course ID for group 1
  • char courseId2 //course ID for group 2
  • int numberOfCourses //to find the average for
    each group
  • double avg1 //average for a course in
    group 1
  • double avg2 //average for a course in
    group 2
  • double avgGroup1 //average group 1
  • double avgGroup2 //average group 2
  • ifstream group1 //input stream variable for
    group 1
  • ifstream group2 //input stream variable for
    group 2
  • ofstream outfile //output stream variable

97
  • calculateAverage
  • To find the course average, we must first find
    the sum of all scores for the course and the
    number of students who took the course, and then
    divide the sum by the number of students.
  • Local Variables (Function calculateAverage)
  • double totalScore //to store the sum of scores
  • int numberOfStudent //to store the number of
    students
  • int score //to read and store a course score

98
  • Algorithm for the function calculateAverage.
  • a. Declare variables
  • b. Initialize totalScore 0.0
  • c. Initialize numberOfStudents to 0.
  • d. Get the (next) course score.
  • e. Update the totalScore by adding the course
    score read in step d.
  • f. Increment numberOfstudents by 1.
  • g. Repeat steps d, e, and f until course score
    equal to 999.
  • h. courseAvg totalScore / numberOfStudent
  • A while loop is used to repeat steps d, e, and f.

99
  • void calculateAverage(ifstream inp,
  • double courseAvg)
  • double totalScore 0.0 //Steps a and b
  • int numberOfStudent 0 //Steps a and c
  • int score //Step a
  • inpgtgtscore //Step d
  • while(score ! -999)
  • totalScore totalScore score //Step e
  • numberOfStudent //Step f
  • inpgtgtscore //step d
  • //end while
  • courseAvg totalScore / numberOfStudent
    //Step h
  • //end calculate Average

100
  • printResult
  • The function printResult prints the groups
    course ID, group number, and course average.
  • The output is stored in a file.
  • We must pass four parameters to this function
    the ofstream variable associated with the output
    file, the group number, the course ID, and the
    course average for the group.
  • if(group number 1)
  • print course id
  • else
  • print a blank
  • print group number and course average

101
  • void printResult(ofstream outp, char courseID,
  • int groupNo, double avg)
  • if(groupNo 1)
  • outpltlt" "ltltcourseIdltlt" "
  • else
  • outpltlt" "
  • outpltltsetw(8)ltltgroupNoltltsetw(15)ltltavgltltendl
  • //end printResult

102
  • Main Algorithm Function main
  • 1. Declare the variables (local declaration).
  • 2. Open the input files.
  • 3. Print a message if you are unable to open a
    file and terminate the program.
  • 4. Open the output file.
  • 5. To output floating-point numbers in a fixed
    decimal format with the decimal point and
    trailing zeros, set the manipulators fixed and
    showpoint. Also, to output floating-point numbers
    to two decimal places, set the precision to two
    decimal places.
  • 6. Initialize the course average for group 1 to
    0.0.
  • 7. Initialize the course average for group 2 to
    0.0.
  • 8. Initialize the number of courses to 0.
  • 9. Print the heading.
  • 10. Get the course ID, courseId1, for group 1.
  • 11. Get the course ID, courseId2, for group 2.

103
  • 12. For each course in group1 and group 2
  • a. if(courseID1 ! courseID2)
  • coutltlt"Data error Course ID's do not
    match.\n")
  • return 1
  • b. else
  • i. Calculate course average for group 1 (call
    function calculateAverage and pass appropriate
    parameters)
  • ii. Calculate course average for group 1 (call
    function calculateAverage and pass appropriate
    parameters)
  • iii. Print results for group 1 (call function
    printResults and pass appropriate parameters.)
  • iv. Print results for group 2 (call function
    printResults and pass appropriate parameters.)
  • v. update average for group 1
  • vi. update average for group 2
  • vii. Increment number of courses

104
  • c. Get the course ID, courseId1, for group 1.
  • d. Get the course ID, courseId2, for group 2.
  • 13. a. if not_end_of_file on group 1 and end-of
    file on group 2
  • print "Ran out of data for group 2
  • before group 1"
  • b. else
  • if end_of_file on group 1 and not_end-of
    file on group 2
  • print "Ran out of data for group 1
  • before group 2"
  • c. else
  • print average of group 1 and group 2.
  • 14. Close input and output files.

105
  • //Program Comparison of Class Averages.
  • include ltiostreamgt
  • include ltiomanipgt
  • include ltfstreamgt
  • using namespace std
  • //function prototypes
  • void calculateAverage(ifstream inp,
  • double courseAvg)
  • void printResult(ofstream outp, char courseId,
  • int groupNo, double avg)
  • int main ()
  • //Step 1
  • char courseId1 //course ID for group 1
  • char courseId2 //course ID for group 2
  • int numberOfCourses

106
  • double avgGroup1 //average group 1
  • double avgGroup2 //average group 2
  • ifstream group1 //input stream variable for
    group 1
  • ifstream group2 //input stream variable for
    group 2
  • ofstream outfile //output stream variable
  • group1.open("agroup1.txt") //Step 2
  • group2.open("agroup2.txt") //Step 2
  • if(!group1 !group2) //Step 3
  • coutltlt"Unable to open files."ltltendl
  • return 1
  • outfile.open("astudent.out") //Step 4
  • outfileltltfixedltltshowpoint //Step 5

107
  • avgGroup1 0.0 //Step 6
  • avgGroup2 0.0 //Step 7
  • numberOfCourses 0 //Step 8
  • //print heading Step 9
  • outfileltlt"Course No Group No Course
    Average"ltltendl
  • group1gtgtcourseId1 //Step 10
  • group2gtgtcourseId2 //Step 11
  • while(group1 group2) //Step 12
  • if(courseId1 ! courseId2) //Step 12a
  • coutltlt"Data error Course IDs do not
    match."ltltendl
  • coutltlt"Program terminates."ltltendl
  • return 1

108
  • else //Step 12b
  • calculateAverage(group1, avg1) //Step 12b.i
  • calculateAverage(group2, avg2) //Step 12b.ii
  • printResult(outfile,courseId1,1,avg1)
  • //Step 12b.iii
  • printResult(outfile,courseId2,2,avg2)
  • //Step 12b.iv
  • avgGroup1 avgGroup1 avg1 //Step 12b.v
  • avgGroup2 avgGroup2 avg2 //Step 12b.vi
  • outfileltltendl
  • numberOfCourses //Step 12b.vii
  • group1gtgtcourseId1 //Step 12c
  • group2gtgtcourseId2 //Step 12d
  • // end while

109
  • if(group1 !group2) //Step 13a
  • coutltlt"Ran out of data for group 2 before group
    1."
  • ltltendl
  • else //Step 13b
  • if(!group1 group2)
  • coutltlt"Ran out of data for group 1 before
    group 2."
  • ltltendl
  • else //Step 13c
  • outfileltlt"Avg for group 1 "
  • ltltavgGroup1 / numberOfCoursesltltendl
  • outfileltlt"Avg for group 2 "
  • ltltavgGroup2 / numberOfCoursesltltend
    l
  • group1.close() //Step 14
  • group2.close() //Step 14
  • outfile.close() //Step 14

110
  • void calculateAverage(ifstream inp,
  • double courseAvg)
  • double totalScore 0.0 //Steps a and b
  • int numberOfStudent 0 //Steps a and c
  • int score //Step a
  • inpgtgtscore //Step d
  • while(score ! -999)
  • totalScore totalScore score //Step e
  • numberOfStudent //Step f
  • inpgtgtscore //step d
  • //end while
  • courseAvg totalScore / numberOfStudent
    //Step h
  • //end calculate Average

111
  • void printResult(ofstream outp, char courseID,
  • int groupNo, double avg)
  • if(groupNo 1)
  • outpltlt" "ltltcourseIdltlt" "
  • else
  • outpltlt" "
  • outpltltsetw(8)ltltgroupNoltltsetw(15)ltltavgltltendl
  • //end printResult

112
  • Sample Output
  • Course No Group No Course Average
  • A 1 62.40
  • 2 61.13
  • B 1 67.91
  • 2 53.30
  • C 1 54.59
  • 2 57.38
  • D 1 65.71
  • 2 64.29
  • E 1 71.80
  • 2 90.00
  • Avg for group 1 64.48
  • Avg for group 2 65.22

113
  • Bar Graph
  • The objective of the second part of the program
    is to display the above results in the form of
    bar graphs as shown below.
  • Course Course Average
  • ID 0 10 20 30 40 50 60 70 80
    90 100
  • ..................................
    ......
  • A
  • B
  • Group 1 --
  • Group 2 --
  • Each symbol ( or ) in the bar graph represents
    2 point. If a course's average is less than 2, no
    symbol is printed.

114
  • printBar
  • We must pass four parameters to this function,
    ofstream variable associated with the output
    file, group number (to print or ), course ID
    and the course average for the department.
  • To print the bar graph, we use a loop to print a
    symbol for each 2 points.
  • If the average is 78.45, we must print 39 symbols
    to represent this sale.
  • To find the number of symbols to be printed, we
    can use integer division as follows
  • numberOfSymbols static_castltintgt(average)/2
  • For example,
  • static_castltintgt(78.45)/2 78/2 39.

115
  • void printResult(ofstream outp, char courseId,
  • int groupNo, double avg)
  • int noOfSymbols
  • int count
  • if(groupNo 1)
  • outpltltsetw(3)ltltcourseIdltlt" "
  • else
  • outpltlt" "
  • noOfSymbols static_castltintgt(avg)/2

116
  • if(groupNo 1)
  • for(count 1 count lt noOfSymbols
    count)
  • outpltlt""
  • else
  • for(count 1 count lt noOfSymbols
    count)
  • outpltlt""
  • outpltltendl
  • //end printResults

117
  • We also include a function, printHeading, to
    print the first two lines of the output. The
    definition of this function is
  • void printHeading(ofstream outp)
  • outpltlt"Course Course Average"ltltendl
  • outpltlt" ID 0 10 20 30 40 50 60
    70"
  • ltlt" 80 90 100"ltltendl
  • outpltlt" ..........................
    .."
  • ltlt"............"ltltendl
  • //end printHeading

118
  • Sample Output
  • Course Class Average
  • ID 0 10 20 30 40 50 60 70 80
    90 100
  • ..................................
    ......
  • A
  • B
  • C
  • D
  • E
Write a Comment
User Comments (0)
About PowerShow.com