Upload
jhilam-biswas
View
234
Download
0
Embed Size (px)
Citation preview
8/2/2019 Class31-32inline Functions Function Overloading
1/27
4/1/2011 Dept.ofCSE
Functionoverloading,default arguments and
inline functions
8/2/2019 Class31-32inline Functions Function Overloading
2/27
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 havingseveral different meanings depending on the context. Using a single function name to perform different
types of tasks is known as function overloading. It isalso referred as function polymorphism.
8/2/2019 Class31-32inline Functions Function Overloading
3/27
4/1/2011 Dept.ofCSE
Function Overloading
Using concept of function overloading- we can design a family of functionswith one function name but withdifferent argument lists.
The function would perform differentoperations depending on theargument list in the function call.
8/2/2019 Class31-32inline Functions Function Overloading
4/27
4/1/2011 Dept.ofCSE
Example#include#includevoid 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);}
8/2/2019 Class31-32inline Functions Function Overloading
5/27
4/1/2011 Dept.ofCSE
Example..
void display(int var){cout
8/2/2019 Class31-32inline Functions Function Overloading
6/27
4/1/2011 Dept.ofCSE
Default arguments
Dept. of CS&E4/1/2011
int fnOpr(int a, int b, int c=12); // fn prototype declarationThis function takes three arguments, of which the last one
has a default of twelve. The programmer may call thisfunction in two ways:result = fnOpr(1, 2, 3); //fn callresult = fnOpr(1, 2); // fn callIn the first case the value for the argument called cis
specified as normal. In the second one, the argument isomitted, and the default value of 12will be used instead.
A default argument is an argument to a function that aprogrammer is not required to specify.
8/2/2019 Class31-32inline Functions Function Overloading
7/27
4/1/2011 Dept.ofCSE
Default argumentsDefault values are given in the function prototypedeclarationWhenever a call is made to a function without specifying anargument, the program will automatically assign values tothe 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 callOnly the trailing arguments can have default values andtherefore we must add defaults from right to left.We cannot provide a default value to a particular argumentin the middle of an argument list.
8/2/2019 Class31-32inline Functions Function Overloading
8/27
4/1/2011 Dept.ofCSE
void sum (int, int, int=6,int=10);void main( ) {int a, b, c, d;couta >>b;sum (a,b);}void sum (int a1, int a2, int a3, int a4) {int temp;temp=a1 + a2 + a3 +a4;cout
8/2/2019 Class31-32inline Functions Function Overloading
9/27
4/1/2011 Dept.ofCSE
outputenter any two nos
11 21sum=48If input is given for c and d, then defaultvalues will not be used.The default arguments should not be
repeated in the function definition.
8/2/2019 Class31-32inline Functions Function Overloading
10/27
4/1/2011 Dept.ofCSE
Default arguments
4/1/2011
void main( ){int x, y;x=fnOpr(1,2,3);y=fnOpr(1,2);cout
8/2/2019 Class31-32inline Functions Function Overloading
11/27
4/1/2011 Dept.ofCSE
Default Arguments
void DisplayStars(int = 10, int =1);void main( ){ DisplayStars( ); cout
8/2/2019 Class31-32inline Functions Function Overloading
12/27
4/1/2011 Dept.ofCSE
Advantage of using functions in a program:
-Saves some memory space.-can be called as many times as needed bythe calling function.Disadvantage:
Every time a function is called, it takes a lot of extra time inexecuting a series of instructions for tasks such as jumping tofunction, saving registers, pushing arguments into the stackreturning to calling function etc..
One solution to this problem is to use macros.
Macros
8/2/2019 Class31-32inline Functions Function Overloading
13/27
4/1/2011 Dept.ofCSE
MACROS: #define statement can be usedto define symbolic constants.#define name textEg. #define PIE 3.1415where name represents a symbolic name(typically in uppercase letters) and text
represents the sequence of characters thatis associated with the symbolic name.
8/2/2019 Class31-32inline Functions Function Overloading
14/27
4/1/2011 Dept.ofCSE
Symbolic constants are replaced bytheir equivalent text.
During compilation process eachoccurrence of a symbolic constant
will be replaced by its correspondingtext.
8/2/2019 Class31-32inline Functions Function Overloading
15/27
4/1/2011 Dept.ofCSE
#definecan also be used to definemacros.i.e. single identifier that is equivalent toexpressions or group of statements.
Macros resemble functions in this sense.
They are defined in an altogether
different manner than functions.
8/2/2019 Class31-32inline Functions Function Overloading
16/27
4/1/2011 Dept.ofCSE
#include#define area length * breadthvoid main(){int length, breadth;cout>length;cout>breadth;cout
8/2/2019 Class31-32inline Functions Function Overloading
17/27
4/1/2011 Dept.ofCSE
Multiline macros are defined by placing a backwardslash(\) at the end of each line except the last line.#include#include#define loop for (lines=1; lines
8/2/2019 Class31-32inline Functions Function Overloading
18/27
4/1/2011 Dept.ofCSE
Differences between macro andfunctions:Macros are sometimes used in place of functions within aprogram. Use of macros are in place of functions within aprogram eliminates the time delays associated with functioncalls.
If a program consists of many repeated function calls, the timesavings resulting from the use of macros can becomesignificant.
8/2/2019 Class31-32inline Functions Function Overloading
19/27
4/1/2011 Dept.ofCSE
On the other hand, macro substitution will take placewherever a reference to a macro appears within a program.
Several references to macro makes the programunreasonably long.The major drawback of macros is that they are not reallyfunctions and hence, the usual error checking does not occurduring program compilation.C++ proposes a new feature called inline functionthatcomes out of all the problems encountered in normalfunctions and macros.
E
8/2/2019 Class31-32inline Functions Function Overloading
20/27
4/1/2011 Dept.ofCSE
Ex:#include#define AND &&void main(){
int marks;cin>>marks;if (marks < 70 AND marks>59)cout
8/2/2019 Class31-32inline Functions Function Overloading
21/27
4/1/2011 Dept.ofCSE
Ex:#include#define AND &define CONDITION (marks < 70 AND marks>59)void main(){int marks;cin>>marks;if (CONDITION)
cout
8/2/2019 Class31-32inline Functions Function Overloading
22/27
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
8/2/2019 Class31-32inline Functions Function Overloading
23/27
4/1/2011 Dept.ofCSE
An inline function is a function that is expanded in line when it isinvoked(or called).
i.e. the compiler replaces the function call with the correspondingfunction code (similar to the macros expansion).
The inline functions are defined as follows:
8/2/2019 Class31-32inline Functions Function Overloading
24/27
4/1/2011 Dept.ofCSE
inlinefunction-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 likedouble c=cube(3.0);double d=cube(2.5+1.5);Expression can also be the argument for inline functions.
8/2/2019 Class31-32inline Functions Function Overloading
25/27
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 becausethe 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 arereproduced at each point where the function is called.
8/2/2019 Class31-32inline Functions Function Overloading
26/27
4/1/2011 Dept.ofCSE
Example program:#include inline float mul(float x, float y){return(x*y);}inline double div(double p, double q){return(p/q);}
void main()
8/2/2019 Class31-32inline Functions Function Overloading
27/27
4/1/2011 Dept.ofCSE
void main(){float a = 12.345;float b = 9.82;cout