Wednesday, May 20, 2009

Inline function

Normally functions saved program space and memory spaces are used because the function is stored only in one place and is only executed when it is called. This concept of function execution may be time consuming since the registers and other processes must be saved before the function gets called.
If the function is short, the programmer may wish to place the code of the function in the calling program itself, in order for it to be executed. This type of function is best handled by the inline function. When the program is compiled, the code present in function body is replaced in the place of function call.

The general form is:

Inline is a keyword prefixed with the function definition.

inline returntype functionname ( argument_list )

{

Body of the function

}


Example:

#include "iostream.h"

#include "conio.h"

inline int big(int a,int b,int c)

{

if((a>c)&&(a>b))
return (a);

else if((b>a)&&(b>c))

return (b);

else if((c>a)&&(c>b))

return (c);

else

return (0);


}

void main()

{
int x,y,z,r;

char ch;

clrscr();

lable:

cout<<"\n\n\n\n\t ENTER THE VALUES FOR X,Y AND Z :\t";

cin>> x>> y>> z;

r=big(x,y,z);

if(r!=0)

cout<<"\n\n\n\t\t BIGGEST NUMBERS IS:\t"<
else

cout<<"\n\n\t\t SAME VALUES ARE REPEATED ";

cout<<"\n\n\t DO YOU WANT TO CONTINUE[Y/N]:\t";

cin>> ch;

if((ch=='y')||(ch=='Y'))

goto lable;

getch();

}

Output:

ENTER X ,Y AND Z VALUE = 8 9 14

BIGGEST NUMBER IS: 14


No comments:

Post a Comment