C++ inline function

C++ Inline Function

one of the objectives of using functions in a program is to save the memory space, which becomes appreciable when a function is likely to be called many times .

however when every time a function is called, it takes a lot of extra time in the executing a series of instructions(statements) for tasks such as jumping to the function, pushing arguments into the stack, saving registers, and returning to the calling function. when a function is small, a substantial percentage of execution time may be spent in such overheads.

one solution of this problem is to use macro definitions, popularly known as macros.
preprocessor macros are popular in c. The major drawback with the macros is that they are not really functions and therefore, The usual error checking does not occur during compilation

C++ programming has a different solution to this problem. To Reduce the cost of calls to small functions, C++ propose a new feature called inline function. it is the compiler replaces the functions call with the corresponding functions code(something similar to macros expansion),An inline function is a function that is expand in line when it is invoked. the inline functions are defined as follows

inline function-header
{
function body
…………
}

example

inline double cube(double x)
{
return(x*x*x);
}

the above inline function can be invoked by statements like

c=cube(3.0);
d=cube(2.5+1.5);

on the execution of these above statements, the values of c variable and d variable will be 27 and 64 respectively.This is make the inline feature far superior to macros. if the arguments are expressions such as 2.5+1.5 the function passes the value of the expression, 4 in this case.

All we need to do is prefix the keyword inline to the function definition.it is easy to make function inline. all the inline functions in program must be defined before they are called.

we should exercise case making a function inline. The speed benefits of inline functions diminish as the function(methods) grows in size. At some point the overhead of the function call becomes small compared to the execution of the function, and the benefits of inline functions may be lost. usually, the functions are made inline whenever they are small enough to be defined in one or two lines in such cases, the use of normal functions will be more meaningful
Example

inline double cube(double x)
{
return(x*x*x);
}

remember that the inline keyword merely sends a request, not a command, to the compiler, The compiler may ignore this request if function definition is too long or too complicated and compiler the function as a normal function.

Some of the conditions where inline expansion may not work here

1. for functions returning values,a switch, if a loop, or a goto exists
2. for functions not return any values, if a return statement exists
3. if functions contain static variable
4. if inline functions are recursive

Note: inline expansion makes a program run fast because the 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 are reproduced at every point where the function is called. so a track if becomes necessary.

Example of inline functions
#include <iostream>
using namespace std;
inline float mul(float x,float y)
{
return (x*y);
}
inline double div(double p, double q)
{
return (p/q);
}
int main()
{
float a=34.67;
float b=9.67;
std::cout<<mul(a,b)<<"\n";
std::cout<<div(a,b)<<"\n";
return 0;
}

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise