1 / 15

Introduction to Function in C++ Programming Language

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

nora
Download Presentation

Introduction to Function in C++ Programming Language

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. 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; 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 <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"; }

  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 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; }

  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 <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

  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? Thank You

More Related