1 / 13

CHAPTER 6 USER-DEFINED FUNCTIONS I

CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn about user-defined functions Examine value-returning functions, including actual and formal parameters

thanos
Télécharger la présentation

CHAPTER 6 USER-DEFINED FUNCTIONS I

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 6USER-DEFINED FUNCTIONS I

  2. In this chapter, you will: • Learn about standard (predefined) functions and discover how to use them in a program • Learn about user-defined functions • Examine value-returning functions, including actual and formal parameters • Explore how to construct and use a value-returning, user-defined function in a program

  3. Functions are like buildingblocks.They allow complicated programs to be divided into manageable pieces. • Some of the advantages of functions are: 1. While working on one function, you can focus on just that part of the program and construct it, debug it, and perfect it. 2. Different people can work on different functions simultaneously. 3. If a function is needed in more than one place in a program, or in different programs, you can write it once and use it many times. • Functions are often referred to as modules. • Functions are like miniature programs. They can be put together to form a larger program.

  4. Pre-defined Functions • Some of the pre-defined mathematical functions are abs(x), sqrt(x), and pow(x,y). • pow(2,3) = 8.0 and pow(2.5,3) = 15.625. • The function pow is of the type doubleor that the function pow returns a value of the type double. • The square root function, sqrt(x), calculates the non-negative square root of x for x >= 0.0. • sqrt(2.25) is 1.5. • The function sqrt is of the type double and has only one parameter. • Math functions are contained in the header file cmath. #include <cctype> toupper('a’) ====> ‘A’ toupper(’*’) ====> ‘*’ toupper(’A’) ====> ‘A’ tolower(’A’) ====> ‘a’

  5. USER-DEFINED FUNCTIONS User-defined functions in C++ • Value-returning functions - functions that have a data type. • Void functions - functions that do not have a data type.

  6. The function abs might have the following definition: int abs(int number) // function heading { if(number < 0) // function body { } number = -number; return number; // value return function } // require a return statement • The variable declared in the heading of the function abs is called the formalparameter of the function abs. • The formal parameter of abs is number. • The function return type is int. • To invoke (call) the function: int num; num = abs(-3); // -3 is the actual parameter

  7. The function “Hello” is a void function:: void Hello(string name) // function heading { // function body { } cout << “Hello, my name is “ << name; } • To invoke (call) the function: string name = “John Smith”; Hello(name); // name is the actual parameter

  8. Formal Parameter: A variable declared in the function heading. • Actual Parameter: A variable or expression listed in a call to a function.

  9. Function Prototype • Function Prototype: Function heading without the body of the function is called a function prototype. Syntax: Function Prototype functionType functionName(parameter list); • Note the semicolon at the end. Example: For the function larger, the function prototype is: void Hello(string name); int abs(int number); • We can rewrite the function prototype of the function larger as follows: void Hello(string ); int abs(int );

  10. //Program: Largest of three numbers #include <iostream> using namespace std; double larger(double x, double y); double compareThree(double x, double y, double z); int main() { double one, two; //Line 1 cout<<"Line 2: Larger of 5 and 10 is " <<larger(5,10)<<endl; //Line 2 cout<<"Line 3: Enter two numbers: "; //Line 3 cin>>one>>two; //Line 4 cout<<endl; //Line 5 cout<<"Line 6: Larger of "<<one<<" and " <<two<<" is "<<larger(one,two)<<endl;//Line 6 cout<<"Line 7: Largest of 23, 34, and 12 is " <<compareThree(23,34,12)<<endl; //Line 7 return 0; }

  11. double larger(double x, double y) { if(x >= y) return x; else return y; } double compareThree (double x, double y, double z) { return larger(x,larger(y,z)); } Sample Run: The user input is in red. Line 2: Larger of 5 and 10 is 10 Line 3: Enter two numbers: 25 73 Line 6: Larger of 25 and 73 is 73 Line 7: Largest of 23, 34, and 12 is 34

  12. Recall that in a value-returning function the return statement returns the value. • Consider the following return statement: return x, y; // only the value y will be returned • This is a legal return statement. • You might think that this return statement is returning the values of x and y. • This is not the case. • A return statement returns only one value, even if the return statement contains more than one expression. • If a return statement contains more than one expression, only the value of the last expression is returned. • Therefore, in the case of the above return statement, the value of y is returned.

More Related