Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC230 Computers & Programming I Lecture Notes 20 Function 5 Dr. Ming Zhang.

Similar presentations


Presentation on theme: "CPSC230 Computers & Programming I Lecture Notes 20 Function 5 Dr. Ming Zhang."— Presentation transcript:

1 CPSC230 Computers & Programming I Lecture Notes 20 Function 5 Dr. Ming Zhang

2 Function 5 ( L20 ) u Functions with Empty Parameter Lists u Inline Functions u Function Call-by-Value u Function Call-by- Reference u Reference for Other Variables u Default Arguments u Unary Scope Resolution Operator (::) u Function Overloading u Exercise/Home Work Dr. Ming Zhang

3 Functions with Empty Parameter Lists u In C++, an empty parameter list is specified by writing either void or nothing at all in parentheses. u The prototype void print( ); specifies that function print does not take any arguments and does not return a value u Examples void function1( ) { cout<<“function1 takes no argument”<<endl;} void function2(void) { cout<<“function2 takes no argument”<<endl;} Dr. Ming Zhang

4 Inline Functions u Function calls involve execution-time overhead. C++ provides inline functions to help reduce function-call overhead - especially for small functions. u The qualifier inline before a function’s return type in the function definition ”advises” the compiler to generate a copy of the function’s code in place (when appropriate) to avoid a function call -saving running time. u Example inline double cube(const double s) { return s*s*s;} Dr. Ming Zhang

5 Function Call-by-Value u When an argument is passed call-by-value, a copy of argument’s value is made and passed to called function. u Changes to the copy do not affect the original variable’s value in the caller. u This prevents the accidental side effects. u Example …... double cube(const double s) { return s*s*s;} …... cin >> side; cout << “Volume of cube is” << cube(side); …... Dr. Ming Zhang

6 Function Call-by- Reference u With call-by-reference, the caller gives the called function the ability to access the caller’s data directly, and to modify that data if the called function so choose. u A reference parameter is an alias for its corresponding argument. u To indicate that a function parameter is passed by reference, simple follow the parameter’s type in the function prototype by an ampersand (&); use the same convention when listing the parameter’s type in the function header. Dr. Ming Zhang

7 Example of Call-by-Reference (Fig.3.20) u …... int x=2, z=4; cout <<x<<squareByValue(x) <<x<< endl; cout <<z; squareByReference(z); cout<<z<<endl; …... int squareByValue( int a) { return a*=a; } //caller’s argument not modified void squareByRefernce( int &cRef) { cRef *= cRef; } // caller’s argument modified u output: 2 4 2 // x x*x x 4 16 16 // z z*z z Dr. Ming Zhang

8 Reference for Other Variables u Reference can also be used as aliases for other variables within a function. u Once a reference is declared as an alias for another variable, all operations supposedly performed on the alias (the reference) are actually performed on the original variable itself. u The alias (the reference) is simply another name for the original variable. u Reference variables mush be initialized in their declarations. Dr. Ming Zhang

9 Example of Reference Variable (Fig. 3.21) u …. int main( ) { int x =3, &y = x; // y is now an alias for x cout <<“x=”<<x<<“ “<<“y=“<<y<<endl; y=7;// y is 7 now cout <<“x=”<<x<<“ “<<“y=“<<y<<endl; return 0; } u Output: x=3 y=3 x=7 y=7 // x is 7 now Dr. Ming Zhang

10 Default Arguments u Function calls may commonly pass a particular value of an argument, the programmer can provide a default value for that argument (default argument). u When a default argument is omitted in a function call, the default value of that argument is automatically inserted by the compiler and passed in the call. u Default arguments can be constant, global variables, or function calls. u Default arguments must be the rightmost (trailing) argument in a function’s parameter list. u The default values should only be defined in the function prototype. Dr. Ming Zhang

11 Example of Default Arguments (Fig. 3.23) int boxVolume(int length=1, int width=1, int height =1); ….. cout <<“Default box volume:” << boxVolume( ); cont<<“\nLength 10, width 1, & height 1:” << boxVolume(10); ….. int boxVolume(int length, int width, int height) { Return length*width*height; } Output: Default box volume: 1 Length 10, width 1& height 1: 10 Dr. Ming Zhang

12 Unary Scope Resolution Operator (::) u It is possible to declare local and global variable of the same name. u C++ provides the unary scope resolution operator(::) to access a global variable when a local variable of the same name is in scope. u The unary scope resolution operator (::) can NOT be used to access a local variable of the same name in an outer block. Dr. Ming Zhang

13 Example of Operator (::) (Fig. 3.24) …... const double PI = 3.14159265358979 int main( ) { const float PI = static_cast<float. ( :: PI); cout << setprecision (20) <<“Local float value of PI =“ << PI <<endl <<Global double value of PI =“ << ::PI <<endl; return 0 } output: Local float value of PI = 3.14159 Global double value of PI = 3.14159265358979 Dr. Ming Zhang

14 Function Overloading u C++ enables several functions of the same name to be defined as long as these function have different sets of parameters (at least their types are concerned). This capability is called function overloading. u Function overloading is commonly used to create several functions of the same name that perform similar tasks, but on different data types. u Overloaded functions can have different return types, but must have different parameter lists. Dr. Ming Zhang

15 Example of Function Overloading(Fig. 3.25) …… int square (int x) { return x*x ;} double square(double y) { return y*y;} int main( ) { cout<<The square of integer 7 is “<< square(7) <<\nThe square of double 7.5 is “<< square(7.5) ; return 0;} Output: The square of integer 7 is 49 The square of double 7.5 is 56.25 Dr. Ming Zhang

16 Question 1 - Exercise/Home Work (1) Write a complete C++ program with the two alternate functions specified below, of which each simply triples the variable count defined in main. Then compare and contrast the two approaches. These two functions are: a) Function tripleCallByValue that passes a copy of count call-by-value, triples the copy and return the new value. B) Function tripleByReference that passes count with true call-by-reference via a reference parameter and triples the original copy of count through its alias (i.e., the reference parameter). Dr. Ming Zhang

17 Question 2 -Exercise/Home Work (1) Write function distance that calculates the distance between two points (x1, y1) and (x2, y2). All numbers and return values should be of type double. Then write a working program to calculate the distance between two points. The user of program could input the values of these two points. Dr. Ming Zhang


Download ppt "CPSC230 Computers & Programming I Lecture Notes 20 Function 5 Dr. Ming Zhang."

Similar presentations


Ads by Google