Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions Modules in C++ are called functions and classes. Main reason to use functions is : – get aid in conceptual organization.

Similar presentations


Presentation on theme: "Functions Modules in C++ are called functions and classes. Main reason to use functions is : – get aid in conceptual organization."— Presentation transcript:

1 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. –Using function name and parameters. Library Functions and User Defined Functions.

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

3 Passing Arguments to Functions Passing Constants Passing Variables –Passing by Value –Passing by Reference

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

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

6 #include 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

7 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. Passing Variables - By Value

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 void frac (float, float&, float&); void main( ) { float num, intpart, fracpart; cout > 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 void square ( ); //function prototype void square (int); void square (int, int); void main( ) { square( ); square (10); square (5,10); }

14 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); } Overloaded Functions

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 #include 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); } Inline Functions

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 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. External or Global Variables

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

21 # include float getavg(float); void main ( ) { float data=1, avg; while (data != 0) { cout > data; avg = getavg(data); cout << “New average is “<<avg<<endl; } Static Variables

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

23 #include 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

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


Download ppt "Functions Modules in C++ are called functions and classes. Main reason to use functions is : – get aid in conceptual organization."

Similar presentations


Ads by Google