Class31-32inline Functions Function Overloading

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 27

Functionoverloading, default arguments and inline functions

4/1/2011

Dept.ofCSE

Function Overloading
A single function name can be used to handle different number and different types of arguments. This is the ability to take more than one form. The behavior depends upon the types of data used in the operation. This is something similar to a particular word having several different meanings depending on the context. Using a single function name to perform different types of tasks is known as function overloading. It is also referred as function polymorphism.

4/1/2011

Dept.ofCSE

Function Overloading
Using concept of function overloading - we can design a family of functions with one function name but with different argument lists. The function would perform different operations depending on the argument list in the function call.
4/1/2011 Dept.ofCSE

Example
#include<iostream.h> #include<conio.h> void display(int); void display(long); void display(char *); void main( ) { int i=10; long j=600000; char *str=HELLO; display(i); display(j); display(str); 4/1/2011 } Dept.ofCSE

Example..
void display(int var) { cout<<var<<endl; } void display(long var) { cout<<var<<endl; } void display(char *var) { cout<<var<<endl; }
4/1/2011 Dept.ofCSE

Default arguments
A default argument is an argument to a function that a programmer is not required to specify. int fnOpr(int a, int b, int c=12); // fn prototype declaration This function takes three arguments, of which the last one has a default of twelve. The programmer may call this function in two ways: result = fnOpr(1, 2, 3); //fn call result = fnOpr(1, 2); // fn call In the first case the value for the argument called c is specified as normal. In the second one, the argument is omitted, and the default value of 12 will be used instead.

4/1/2011

Dept. of CS&E Dept.ofCSE

Default values are given in the function prototype declaration Whenever a call is made to a function without specifying an argument, the program will automatically assign values to the parameters. e.g: float amount(float principal, int period, float rate=0.15); Default argument is checked for type at the time of declaration and evaluated at the time of call Only the trailing arguments can have default values and therefore we must add defaults from right to left. We cannot provide a default value to a particular argument in the middle of an argument list.

Default arguments

4/1/2011

Dept.ofCSE

void sum (int, int, int=6,int=10); void main( ) { int a, b, c, d; cout<<enter any two nos.\n; cin >>a >>b; sum (a,b); } void sum (int a1, int a2, int a3, int a4) { int temp; temp=a1 + a2 + a3 +a4; cout << sum= << temp; }

4/1/2011

Dept.ofCSE

output enter any two nos 11 21 sum=48 If input is given for c and d, then default values will not be used. The default arguments should not be repeated in the function definition.

4/1/2011

Dept.ofCSE

Default arguments
int fnOpr(int a, int b, int c =12);

int fnOpr(int a, int b, int c) { int sum; sum = a + b + c; return (sum); Output: } x = 6 & y = 15 void main( ){ int x, y; x=fnOpr(1,2,3); y=fnOpr(1,2); cout<<"\nx= " <<x<<" & y= "<<y; }
4/1/2011 Dept.ofCSE

Default Arguments
void DisplayStars(int = 10, int =1); void main( ) { DisplayStars( ); cout << endl; DisplayStars(5); cout << endl; DisplayStars(7,3); }

void DisplayStars(int Cols, int Rows) { for (int Down = 0; Down < Rows; Down++) { for (int Across=0; Across < Cols;Across++) cout << *; cout << endl; } }

4/1/2011

Dept.ofCSE

Macros
Advantage of using functions in a program: -Saves some memory space. -can be called as many times as needed by the calling function. Disadvantage: Every time a function is called, it takes a lot of extra time in executing a series of instructions for tasks such as jumping to function, saving registers, pushing arguments into the stack returning to calling function etc..
One solution to this problem is to use macros.

4/1/2011

Dept.ofCSE

MACROS: #define statement can be used to define symbolic constants. #define name text
Eg. #define PIE 3.1415 where name represents a symbolic name (typically in uppercase letters) and text represents the sequence of characters that is associated with the symbolic name.
4/1/2011 Dept.ofCSE

Symbolic constants are replaced by their equivalent text. During compilation process each occurrence of a symbolic constant will be replaced by its corresponding text.

4/1/2011

Dept.ofCSE

#define can also be used to define macros. i.e. single identifier that is equivalent to expressions or group of statements. Macros resemble functions in this sense.

They are defined in an altogether different manner than functions.


4/1/2011 Dept.ofCSE

#include<iostream.h> #define area length * breadth void main() { int length, breadth; cout<< Length=; cin>>length; cout<< Breadth=; cin>>breadth; cout<< Area= << area; } Macros definitions are placed at the beginning of a file, ahead of the first function definition.
4/1/2011 Dept.ofCSE

Multiline macros are defined by placing a backward slash(\) at the end of each line except the last line. #include<iostream.h> #include<stdio.h> #define loop for (lines=1; lines<=n; lines++) { \ cout<<*; \ } void main() { int lines,n; cout<< Number of lines=; cin>>n; loop }
4/1/2011 Dept.ofCSE

Differences between macro and functions:


Macros are sometimes used in place of functions within a program. Use of macros are in place of functions within a program eliminates the time delays associated with function calls. If a program consists of many repeated function calls, the time savings resulting from the use of macros can become significant.

4/1/2011

Dept.ofCSE

On the other hand, macro substitution will take place wherever a reference to a macro appears within a program. Several references to macro makes the program unreasonably long. The major drawback of macros is that they are not really functions and hence, the usual error checking does not occur during program compilation. C++ proposes a new feature called inline function that comes out of all the problems encountered in normal functions and macros.

4/1/2011

Dept.ofCSE

Ex:
#include<iostream.h> #define AND && void main() { int marks; cin>>marks; if (marks < 70 AND marks>59) cout<<First class; else cout<< Other than first class; }

4/1/2011

Dept.ofCSE

Ex:
#include<iostream.h> #define AND && #define CONDITION (marks < 70 AND marks>59) void main() { int marks; cin>>marks; if (CONDITION) cout<<First class; else cout<< Other than first class; }

4/1/2011

Dept.ofCSE

Macros can have arguments just as functions:


#define AREA(x) (3.1415*x*x) void main() { float rad1= 5.25,rad2=6.25,a; a=AREA(rad1); cout<<The area of the circle with radius <<rad1 << is <<a; a=AREA(rad2); cout<<The area of the circle with radius <<rad2 << is <<a; }

4/1/2011

Dept.ofCSE

An inline function is a function that is expanded in line when it is invoked(or called). i.e. the compiler replaces the function call with the corresponding function code (similar to the macros expansion).

4/1/2011

Dept.ofCSE

The inline functions are defined as follows: inline function-header { Function body } The example goes like this:
inline double cube(double a) { return(a*a*a); }

The above inline function can be invoked by statements like double c=cube(3.0); double d=cube(2.5+1.5); Expression can also be the argument for inline functions.

4/1/2011

Dept.ofCSE

All inline functions must be defined before they are called.


It is easy to make a function as inline function. Just prefix the keyword inline to the function definition. The inline expansion makes a program run faster because the overhead of a function call and return is eliminated. However, it makes the program to take up more memory because the statements that define the inline function are reproduced at each point where the function is called.

4/1/2011

Dept.ofCSE

Example program:
#include <iostream.h> inline float mul(float x, float y) { return(x*y); } inline double div(double p, double q) { return(p/q); }

4/1/2011

Dept.ofCSE

void main() { float a = 12.345; float b = 9.82; cout << mul(a,b)<<\n; cout << div(a,b)<<\n; getch(); }

The O/P of this program will be 121.228 1.25713

4/1/2011

Dept.ofCSE

You might also like