Friday, 8 March 2013

C++ Inline Functions


C++ Inline Functions

What is Inline Function?

Inline functions are functions where the call is made to inline functions. The actual code then gets placed in the calling program.

Reason for the need of Inline Function:

Normally, a function call transfers the control from the calling program to the function and after the execution of the program returns the control back to the calling program after the function call. These concepts of function save program space and memory space and are used because the function is stored only in one place and is only executed when it is called. This execution may be time consuming since the registers and other processes must be saved before the function gets called.
The extra time needed and the process of saving is valid for larger functions. If the function is short, the programmer may wish to place the code of the function in the calling program in order for it to be executed. This type of function is best handled by the inline function. In this situation, the programmer may be wondering "why not write the short code repeatedly inside the program wherever needed instead of going for inline function?". Although this could accomplish the task, the problem lies in the loss of clarity of the program. If the programmer repeats the same code many times, there will be a loss of clarity in the program. The alternative approach is to allow inline functions to achieve the same purpose, with the concept of functions.

What happens when an inline function is written?

The inline function takes the format as a normal function but when it is compiled it is compiled as inline code. The function is placed separately as inline function, thus adding readability to the source program. When the program is compiled, the code present in function body is replaced in the place of function call.

General Format of inline Function:

The general format of inline function is as follows:
inline datatype function_name(arguments)
The keyword inline specified in the above example, designates the function as inline function. For example, if a programmer wishes to have a function named exforsys with return value as integer and with no arguments as inline it is written as follows:
inline int exforsys( )

Example:

The concept of inline functions:
Sample Code
1.  #include <iostream>
2.  using namespace std; 
3.  int exforsys(int);
4.  void main( )
5.  {
6.          int x;
7.          cout << "n Enter the Input Value: ";
8.          cin>>x;
9.          cout << "n The Output is: " << exforsys(x);
10.     } 
11.      
12.     inline int exforsys(int x1)
13.     {
14.             return 5*x1;
15.     }
Copyright exforsys.com


The output of the above program is:
Description: http://www.exforsys.com/images/cpp/26_1.jpg
The output would be the same even when the inline function is written solely as a function. The concept, however, is different. When the program is compiled, the code present in the inline function exforsys( ) is replaced in the place of function call in the calling program. The concept of inline function is used in this example because the function is a small line of code.
To write a program to find the multiplication values and the cubic values using inline function.

ALGORITHM:

Step 1: Start the pogram.
Step 2: Declare the class.
Step 3: Declare and define the inline function for multiplication and cube.
Step 4: Declare the class object and variables.
Step 5: Read two values.
Step 6: Call the multiplication and cubic functions using class objects.
Step 7: Return the values.
Step 8: Display.
Step 9: Stop the program.

PROGRAM:

#include<iostream.h>
#include<conio.h>

class line
{
   public:
              inline float mul(float x,float y)
              {
                            return(x*y);
              }
              inline float cube(float x)
              {
                            return(x*x*x);
              }
};

void main()
{              line obj;
              float val1,val2;
              clrscr();
              cout<<"Enter two values:";
              cin>>val1>>val2;
              cout<<"\nMultiplication value is:"<<obj.mul(val1,val2);
              cout<<"\n\nCube value is          :"<<obj.cube(val1)<<"\t"<<obj.cube(val2);
             getch();
}Output:
             Enter two values: 5  7
              Multiplication Value is: 35
              Cube Value is: 25 and 343

No comments:

Post a Comment