1 / 24

Functions vustudents.ning

Functions http://vustudents.ning.com. Modules in C++ are called functions and classes . Main reason to use functions is : get aid in conceptual organization of program. reduce program size. avoid repetition of code. A function is invoked by a function call.

Télécharger la présentation

Functions vustudents.ning

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. Functionshttp://vustudents.ning.com Modules in C++ are called functions and classes. Main reason to use functions is : get aid in conceptual organization of program. reduce program size. avoid repetition of code. A function is invoked by a function call. Using function name and parameters. Library Functions and User Defined Functions.

  2. Function Definitions #include <iostream.h> int square ( ); //function prototype void main( ) { cout << square( ); } int square ( ) //function definition { return 5 * 5; }

  3. Passing Arguments to Functions • Passing Constants • Passing Variables • Passing by Value • Passing by Reference

  4. Passing Constants to Function #include <iostream.h> int square (int); //function prototype void main( ) { cout << square( 5); } int square (int y) //function definition { return y * y; }

  5. Passing Variables - By Value • Function creates new variables to hold the values passed. • Initialize these variables with the values passed. • Effect on actual variables?

  6. Passing Variables - By Value #include <iostream.h> int square (int); //function prototype void main( ) { int x ; cout << “\nEnter a number : ”; cin > x; cout << square(x); } int square (int y) //function definition { return y *y; }

  7. Passing Variables - By Value • Rewrite the previous program to get square of numbers from 1 to 10. • Write a program using a function max( ) to determine and return the largest of three integers entered by the user.

  8. Returning Values from Functions • The return Statement. • Default return type - int. • No value returned, if return type is void. • If many arguments sent to functions - number of values returned ?

  9. Passing Variables - By Reference • A reference to original value is passed. • Function has an access to actual variables in calling program. • Provides a mechanism to return more than one value from the function.

  10. Passing Variables - By Reference # include <iostream.h> void frac (float, float&, float&); void main( ) { float num, intpart, fracpart; cout<<“\nEnter number ”; cin >> num; frac(num, intpart, fracpart); cout << “\nInteger part is “ <<intpart <<“\nFraction part is “ <<fracpart; }

  11. Passing Variables - By Reference void frac (float n, float& intp, float& fracp) { intp = float( long(n) ); fracp = n - intp; } // ampersand (&) is not used in function call // In function call,no difference in value or // reference parameters.

  12. Overloaded Functions • Same • Function name, return type . • Different • Number of Arguments or • Type of Arguments

  13. Overloaded Functions #include <iostream.h> void square ( ); //function prototype void square (int); void square (int, int); void main( ) { square( ); square (10); square (5,10); }

  14. Overloaded Functions void square ( ) { cout << endl<<5 * 5; } void square (int y) { cout << endl<<y * y; } void square (int s, int f) { for (; s <=f; s++) cout << endl <<(s * s); }

  15. Inline Functions • Written like a normal function, but compiles into inline code instead of into a function. • Compiler must have seen the function(not just the declaration) before first function call. • Definition of inline function before main( ). • inline keyword is a request to compiler, which may be ignored.

  16. Inline Functions #include <iostream.h> inline float lbstokg ( float pounds) { return 0.453592 * pounds; } void main ( ) { float lbs; cout << “\nEnter weight in pounds : “; cin >> lbs; cout <<“Weight in kilograms is “ << lbstokg(lbs); }

  17. Variables and Storage Classes • Lifetime • The time period between the creation and destruction of a variable is called its lifetime or duration. • Visibility • A variable’s visibility describes where within a program it can be accessed. • Scope • is that part of the program where the variable is visible.

  18. Automatic Variables • An automatic variable is not created until the function in which it is defined is called. • The word automatic is used as variables are automatically created when a function is called and auto destroyed when it returns. • Lifetime of auto var coincides with the time when function is executing. • Visible only within the function in which they are defined. • No initialization of auto/local variables.

  19. External or Global Variables • Defined outside of any function. • Can be accessed by any function. • Exist for life of the program. • Visible in the file in which they are defined.

  20. Static Variables • Defined within a function with keyword static. • Visibility of a local variable. • Lifetime of an external variable. • Initialization takes place only once.

  21. Static Variables # include <iostream.h> float getavg(float); void main ( ) { float data=1, avg; while (data != 0) { cout << “Enter a number ”; cin >> data; avg = getavg(data); cout << “New average is “<<avg<<endl; } }

  22. Static Variables float getavg(float newdata) { static float total=0; static int count = 0; count++; total += newdata; return total / count; }

  23. Recursive Function - Function calling itself #include <iostream.h> unsigned long factorial(unsigned long); void main ( ) { for (int I=0; I <=10; I++) cout <<endl << “Factorial of “ << I << “ = “ <<factorial(I); }

  24. Recursive Function - Function calling itself unsigned long factorial(unsigned long num) { if (num <=1) return 1; else return (num * factorial (num-1)); } http://vustudents.ning.com

More Related