Inline Function

 

Inline function:

Inline function is a function whose code is placed at its call.

Syntax:

inline return_type function_name(arguments)

{

     //function code

}                                                             

 

Characteristics:

·       Inline function definition starts with keyword ‘inline’.

·       The inline function should be defined before all the functions that call it.

·       The compiler replaces the function call statement with the function code itself.

·       Inline functions run faster than normal functions.

 

Advantages:

·       These functions are compact function calls

·       Size of object code is less

·       Speed of execution of the program is faster

·       Very efficient code can be generated

·       The readability of the program is increases

 

Example: Write a program to find cube of a number using Inline function.

#include<iostream.h>

#include<conio.h>

inline int cube(int a)

{

     return (a*a*a);

}

void main()

{

      int x,y;

      clrscr();

      cout<<”enter a number”;

      cin>>x;

      y=cube(x);

      cout<<”cube =”<<y;

      getch();

}

 

Comments