13

Click here to load reader

Functions

Embed Size (px)

Citation preview

Page 1: Functions

1 . FUNCTION OVERLOADING

2 . PASSING DEFAULT ARGUMENTS

3 . INLINE FUNCTION

Functions

Compiled By: Kamal Acharya

Page 2: Functions

Function Overloading

When the function with the same name perform the multiple task it is called as the function overloading.

Example: If the function add() can add not only the integers but also the floats and strings it is overloaded.

The functions differ in one of the following things Number of parameters

Data types of parameters

Order of appearance

Compiled By: Kamal Acharya

Page 3: Functions

Number Of Arguments

#include<iostream.h>

#include<conio.h>

void print(char);

void print(char, int);

int main()

{

clrscr();

print('X');

print('Z',12);

getch();

return 0;

}

void print(char a)

{

cout<<"In the Single argument function"<<endl;

cout<<a<<endl;

}

void print(char a, int b)

{

int i;

cout<<"In the double argument function"<<endl;

for(i=0;i<b;i++)

{

cout<<a<<"\t";

}

}

Compiled By: Kamal Acharya

Page 4: Functions

Data Types of Parameters

#include<iostream.h>

#include<conio.h>

void add(int , int);

void add(float, float);

int main()

{

clrscr();

add(2,6);

add(12.6,1.2);

getch();

return 0;

}

void add(int a, int b)

{

cout<<"In the integer addition function"<<endl;

cout<<a+b<<endl;

}

void add(float a, float b)

{

cout<<"In the float addition function"<<endl;

cout<<a+b<<endl;

}

Compiled By: Kamal Acharya

Page 5: Functions

Oreder Of Appearance

#include<iostream.h>

#include<conio.h>

void fun(int , char);

void fun(char, int);

int main()

{

clrscr();

fun(2,'A');

fun('B',1);

getch();

return 0;

}

void fun(int a, char b)

{

cout<<"In the function with first argument integer and the second character"<<endl;

cout<<"The first argument is"<< a<<".The second argument is "<< b<<endl;

}

void fun(char a, int b)

{

cout<<"In the function with first argument character and the second integer"<<endl;

cout<<"The first argument is"<< a<<".The second argument is "<< b<<endl;

} Compiled By: Kamal Acharya

Page 6: Functions

Passing Default Arguments

• Allows programmer to define a default behavior

A value for a parameter can be implicitly passed

Reduces need for similar functions that differ only in the number of parameters accepted

Compiled By: Kamal Acharya

Page 7: Functions

Default parameters must appear after any mandatory parameters

Bad example void Trouble(int x = 5, double z, double y) {

...

}

Cannot come before

mandatory parameters

Compiled By: Kamal Acharya

Page 8: Functions

Consider

void PrintChar(char c = '=', int n = 80)

{

for (int i = 0; i < n; ++i)

cout << c;

}

What happens in the following invocations?

PrintChar('*', 20);

PrintChar('-');

PrintChar();

Compiled By: Kamal Acharya

Page 9: Functions

Program to Illustrate Default Arguments

#include<iostream.h>

#include<conio.h>

void print(int a= 10 , char b='B');

int main()

{

clrscr();

print();

print(2);

print(1,'Z');

getch();

return 0;

}

void print(int a, char b)

{

int i;

for(i=0;i<a;i++)

{

cout<<b<<"\t";

}

}

Compiled By: Kamal Acharya

Page 10: Functions

Inline Function

Reduce function call overhead—especially for small functions.

Qualifier inline before a function’s return type in the function definition

“Advises” the compiler to generate a copy of the function’s code in place (when appropriate) to avoid a function call.

Compiled By: Kamal Acharya

Page 11: Functions

Multiple copies of the function code are inserted in the program (often making the program larger).

The compiler can ignore the inline qualifier and typically does so for all but the smallest functions.

Compiled By: Kamal Acharya

Page 12: Functions

Syntax of Inline Function

inline returnType functionName(arguments);

Example:

inline int add(int a, int b);

Compiled By: Kamal Acharya

Page 13: Functions

Program to Illustrate inline Function

#include<iostream.h>

#include<conio.h>

inline float mul(float x, float y)

{

return(x*y);

}

inline double div(double p, double q)

{

return(p/q);

}

int main()

{

clrscr();

float a=12.12;

float b=2.2;

cout<<mul(a,b)<<"\n";

cout<<div(a,b)<<"\n";

getch();

return 0;

}

Compiled By: Kamal Acharya