You are on page 1of 12

INLINE FUNCTION

| Dr. Yasir | Programming Domain


NORMAL VS INLINE FUNCTON
Inside the main() method, when the function is called, the
control is transferred to the definition of the called function.
 The addresses from where the function is called and the
definition of the function are different.
 This control transfer takes a lot of time and increases the
overhead.
When the inline function is encountered, then the definition of
the function is copied to it. In this case, there is no control
transfer which saves a lot of time and also decreases the
overhead
INLINE FUNCTON
• If
make a function as inline, then the compiler replaces the
function calling location with the definition of the inline
function at compile time.
• Any changes made to an inline function will require the inline
function to be recompiled again because the compiler would
need to replace all the code with a new code; otherwise, it will
execute the old functionality.
SYNTAX FOR AN INLINE FUNCTION:
inline return_type function_name(parameters)
{
// function code?
}
#include <iostream>
using namespace std;
inline int add(int a, int b)
{
return(a+b);
}
int main()
{
cout<<"Addition of 'a' and 'b' is:"<< add(2,3);
}
• Once the compilation is done, the code would be like :
inline int add(int a, int b)
{
return(a+b);
}
int main()
{
cout<<"Addition of 'a' and 'b' is:"<<return(2+3);
return 0;
}
WHY DO WE NEED AN INLINE FUNCTION IN C++?
• The main use of the inline function in C++ is to save memory
space.
• Whenever the function is called, then it takes a lot of time to
execute the tasks, such as moving to the calling function.
• If the length of the function is small, less time would be taken
for execution
• An inline function can be used when the performance is
required.
INLINE FUNCTION AND CLASSES
• It is also possible to define the inline function inside the class.
• Infact, all the functions defined inside the class are implicitly
inline.
• If
you need to explicitly declare inline function in the class then
just declare the function inside the class and define it outside
the class using inline keyword.
class S
{
public:
inline int square(int s) // redundant use of inline
{
// this function is automatically inline
// function body
}
};
• The above style is considered as a bad programming style. The best
programming style is to just write the prototype of function inside the
class and specify it as an inline in the function definition.
MAKING FUNCTION INLINE OUTSIDE CLASS
class S
{
public:
int square(int s); // declare the function
};

inline int S::square(int s) // use inline prefix


{

}
IMPLEMENTATION
#include<iostream> inline void operation :: sum() { add =
using namespace std; a+b;
class operation{ cout << "Addition of two numbers: " <<
a+b << "\n";
int a, b, add;
}
float div; int main()
public: {
void get(); operation s;
void sum(); s.get();
}; s.sum();
inline void operation :: get() }
{ cout << "Enter first value:"; cin
>> a;
cout << "Enter second value:"; cin
>> b;

You might also like