240 likes | 328 Views
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.
E N D
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.
Function Definitions #include <iostream.h> int square ( ); //function prototype void main( ) { cout << square( ); } int square ( ) //function definition { return 5 * 5; }
Passing Arguments to Functions • Passing Constants • Passing Variables • Passing by Value • Passing by Reference
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; }
Passing Variables - By Value • Function creates new variables to hold the values passed. • Initialize these variables with the values passed. • Effect on actual variables?
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; }
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.
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 ?
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.
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; }
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.
Overloaded Functions • Same • Function name, return type . • Different • Number of Arguments or • Type of Arguments
Overloaded Functions #include <iostream.h> void square ( ); //function prototype void square (int); void square (int, int); void main( ) { square( ); square (10); square (5,10); }
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); }
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.
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); }
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.
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.
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.
Static Variables • Defined within a function with keyword static. • Visibility of a local variable. • Lifetime of an external variable. • Initialization takes place only once.
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; } }
Static Variables float getavg(float newdata) { static float total=0; static int count = 0; count++; total += newdata; return total / count; }
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); }
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