Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Function in C++ Programming Language LeMoyne-Owen College Chen-Huei Chou University of Wisconsin-Milwaukee April 4, 2008.

Similar presentations


Presentation on theme: "Introduction to Function in C++ Programming Language LeMoyne-Owen College Chen-Huei Chou University of Wisconsin-Milwaukee April 4, 2008."— Presentation transcript:

1 Introduction to Function in C++ Programming Language LeMoyne-Owen College Chen-Huei Chou University of Wisconsin-Milwaukee April 4, 2008

2 What is a function? A function is a subprogram that acts on data and often returns a value. Advantages of using functions  Program re-use  Structural programming  Team programming  Easy for maintenance and debugging Your most familiar function: main()

3 Main function int main(void) { statement; function1(); statement; function2(); statement; return 0; }

4 Function Definition function-type function-name( parameter- list ) { local-definitions; function-implementation; } function-type  with returned value: int, float, char  without returned value: void function-name: same rule of naming

5 Function Definition (cont.) parameter-list: formal parameters of the function together with their types local-definitions:  definitions of variables that are used in the function-implementation.  no meaning outside the function function-implementation:  executable statements that implement the effect of the function

6 Where to put function? Before main function void function1(void) { statements } void main() { function1(); }

7 Where to put function? (cont.) After main function with function prototype void function1(void); //prototype of function1 void main() { function1(); } void function1(void) { statements }

8 Types of Functions Functions with no parameters Functions with parameters and no return value Functions that return values

9 Functions with no parameters Of limited use Example: skip 3 lines #include void skipthree(void) // Function to skip three lines { cout << endl << endl << endl; } void main() { int....; float....; cout << "Title Line 1"; skipthree(); cout << "Title Line 2"; }

10 Functions with parameters and no return value Example: skip n lines void skip(int n) // Function skips n lines on output { int i; // a local variable to this function // now loop n times for (i = 0; i < n; i++) cout << endl; } void main() { int m = 6, n = 3;...............; skip(m);.......; skip(m + n);............; skip(4);.......; }

11 Functions that return values Example: distance from the origin to p float distance(float x, float y) // Returns the distance of (x, y) from origin { float dist; //local variable dist = sqrt(x * x + y * y); return dist; } x y 0 p Sqrt(x 2 +y 2 )

12 Hands-on practice Please implement a program which can calculate the distance from origin by using function. Hint: float distance(float x, float y)

13 #include // Function prototypes float distance(float,float); void main() { float x,y,dist; // Test function distance(x,y) cout << "Testing function distance(x,y)" << endl; cout << "Enter values for x and y: "; cin >> x >> y; dist = distance(x,y); cout << "Distance of (" << x << ',' << y << ") from origin is " << dist << endl << "Tested" << endl; } // End of main

14 // Function Definitions float distance(float x, float y) // Returns the distance of (x, y) from origin { float dist; //local variable dist = sqrt(x * x + y * y); return dist; }

15 Any Question?


Download ppt "Introduction to Function in C++ Programming Language LeMoyne-Owen College Chen-Huei Chou University of Wisconsin-Milwaukee April 4, 2008."

Similar presentations


Ads by Google