1 / 118

CHAPTER 7 USER-DEFINED FUNCTIONS II

CHAPTER 7 USER-DEFINED FUNCTIONS II. 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

pennie
Download Presentation

CHAPTER 7 USER-DEFINED FUNCTIONS II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 <iostream> using namespace std; void printStars(void); int main() { printStars(); //Line 1 cout<<"********** Annual ***********"<<endl; //Line 2 printStars(); //Line 3 cout<<"******* Spring Sale **********"<<endl; //Line 4 printStars(); //Line 5 return 0; } void printStars(void) { cout<<"******************************"<<endl; cout<<"******************************"<<endl; }

  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 <= 2; lines++) //Line 1 { for(stars = 1; stars <= 30; stars++) //Line 2 cout<<"*"; //Line 3 cout<<endl; //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 <iostream> using namespace std; void printStars(int blanks, int starsInLine); int main() { int numberOfLines; int counter; int numberOfBlanks; cout<<"Enter the number of star lines (1 to 20)" <<" to be printed--> "; //Line 1 cin>>numberOfLines; //Line 2

  15. while(numberOfLines < 0 || numberOfLines > 20) //Line 3 { cout<<"Number of star lines should be " <<"between 1 and 20"<<endl; //Line 4 cout<<"Enter the number of star lines " <<"(1 to 20)to be printed--> "; //Line 5 cin>>numberOfLines; //Line 6 } cout<<endl<<endl; //Line 7 numberOfBlanks = 30; //Line 8 for(counter = 1; counter <= 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 <= blanks; count++) //Line 13 cout<<" "; //Line 14 for(count = 1; count <= starsInLine; count++) //Line 15 cout<<" *"; //Line 16 cout<<endl; }

  17. Sample Run: The user input is in red. Enter the number of star lines (1 to 20) to be printed--> 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 <iostream> using namespace std; void getScore(int& score); void printGrade(int score); int main () { int courseScore; cout<<"Line 1: Based on the course score, this program " <<"computes the course grade."<<endl; //Line 1

  21. getScore(courseScore); //Line 2 printGrade(courseScore); //Line 3 return 0; } void getScore(int& score) { cout<<"Line 4: Enter course score--> "; //Line 4 cin>>score; //Line 5 cout<<endl<<"Line 6: Course score is " <<score<<endl; //Line 6 }

  22. void printGrade(int score) { cout<<"Line 7: Your grade for the course is "; //Line 7 if(score >= 90) //Line 8 cout<<"A"<<endl; else if(score >= 80) cout<<"B"<<endl; else if(score >= 70) cout<<"C"<<endl; else if(score >= 60) cout<<"D"<<endl; else cout<<"F"<<endl; }

  23. Sample Run: Line 1: Based on the course score, this program computes the course grade. Line 4: Enter course score--> 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 <iostream> 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. cout<<"Line 4: Inside main: num1 = "<<num1 <<", num2 = "<<num2<<", and ch = "<<ch<<endl; //Line 4 funOne(num1, num2, ch); //Line 5 cout<<"Line 6: After funOne: num1 = "<<num1 <<", num2 = "<<num2<<", and ch = "<<ch<<endl; //Line 6 funTwo(num2, 25, ch); //Line 7 cout<<"Line 8: After funTwo: num1 = "<<num1 <<", num2 = "<<num2<<", and ch = "<<ch<<endl; //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 cout<<"Line 13: Inside funOne: a = "<<a<<", b = "<<b <<", v = "<<v<<", and one = "<<one<<endl;//Line 13 } void funTwo(int& x, int y, char& w) { x++; //Line 14 y = y * 2; //Line 15 w = 'G'; //Line 16 cout<<"Line 17: Inside funTwo: x = "<<x <<", y = "<<y <<", and w = "<<w<<endl;//Line 17 }

  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. cout<<"Line 4: Inside main: num1 = "<<num1 <<", num2 = "<<num2<<", and ch = "<<ch<<endl; //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. cout<<"Line 13: Inside funOne: a = "<<a<<", b = "<<b <<", v = "<<v<<", and one = "<<one<<endl;//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. cout<<"Line 6: After funOne: num1 = "<<num1 <<", num2 = "<<num2<<", and ch = "<<ch<<endl; //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. cout<<"Line 17: Inside funTwo: x = "<<x <<", y = "<<y <<", and w = "<<w<<endl;//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. cout<<"Line 8: After funTwo: num1 = "<<num1 <<", num2 = "<<num2<<", and ch = "<<ch<<endl; //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 <iostream> 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; cout<<"Line 1: Inside main: num = "<<num<<endl; //Line 1 addFirst(num,num); //Line 2 cout<<"Line 3: Inside main after addFirst:" <<" num = " <<num<<endl; //Line 3 doubleFirst(num,num); //Line 4 cout<<"Line 5: Inside main after " <<"doubleFirst: num = "<<num<<endl; //Line 5 squareFirst(num,num); //Line 6 cout<<"Line 7: Inside main after " <<"squareFirst: num = "<<num<<endl;//Line 7 return 0; }

  48. void addFirst(int& first, int& second) { cout<<"Line 8: Inside addFirst: first = "<<first <<", second = "<<second<<endl; //Line 8 first = first + 2; //Line 9 cout<<"Line 10: Inside addFirst: first = "<<first <<", second = "<<second<<endl; //Line 10 second = second * 2; //Line 11 cout<<"Line 12: Inside addFirst: first = "<<first <<", second = "<<second<<endl; //Line 12 }

  49. void doubleFirst(int one, int two) { cout<<"Line 13: Inside doubleFirst: one = "<<one <<", two = "<<two<<endl; //Line 13 one = one * 2; //Line 14 cout<<"Line 15: Inside doubleFirst: one = "<<one <<", two = "<<two<<endl; //Line 15 two = two + 2; //Line 16 cout<<"Line 17: Inside doubleFirst: one = "<<one <<", two = "<<two<<endl; //Line 17 }

  50. void squareFirst(int& ref, int val) { cout<<"Line 18: Inside squareFirst: ref = " <<ref <<", val = "<< val<<endl; //Line 18 ref = ref * ref; //Line 19 cout<<"Line 20: Inside squareFirst: ref = " <<ref <<", val = "<< val<<endl; //Line 20 val = val + 2; //Line 21 cout<<"Line 22: Inside squareFirst: ref = " <<ref <<", val = "<< val<<endl; //Line 22 }

More Related