210 likes | 538 Views
Introduction to Function in C++ Programming Language. LeMoyne-Owen College Chen-Huei Chou University of Wisconsin-Milwaukee April 4, 2008. 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
E N D
Introduction to Function in C++ Programming Language LeMoyne-Owen College Chen-Huei Chou University of Wisconsin-Milwaukee April 4, 2008
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()
Main function int main(void) { statement; statement; function1(); statement; function2(); statement; return 0; }
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
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
Where to put function? Before main function void function1(void) { statements } void main() { function1(); }
Where to put function? (cont.) After main function with function prototype void function1(void); //prototype of function1 void main() { function1(); } void function1(void) { statements }
Types of Functions Functions with no parameters Functions with parameters and no return value Functions that return values
Functions with no parameters Of limited use Example: skip 3 lines #include <iostream.h> 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"; }
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); .......; }
Functions that return values p Sqrt(x2+y2) y 0 x 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; }
Hands-on practice • Please implement a program which can calculate the distance from origin by using function. • Hint: float distance(float x, float y)
#include <iostream.h> #include <iomanip.h> #include <math.h> // 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
// 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; }
Any Question? Thank You