What is Inline function in C++

What is Inline Function in C++?

so so

Namaste, My name is Nitin Kumar and I am here to write a blog on the topic of Inline function in C++. We will discuss all the important factors of the Inline function in c++. We will discuss all about the Inline function,  why it is used where it is used when it is used etc.so

So lets began our one of the most important topic of the C++ Inline function in C++.

Inline Function in C++ 

In Inline function, the definition part of the main function is copied into calling the function. so

Inline Keyword is used for defining an inline function. In simple function when we call any function the control passes from calling function to called function. so

But in inline function when we call any function the control passes from called function to calling the function. Compilation time is more to a simple function. so 

The syntax for declaring an inline function in C++ 

inline function_type(parameter list);

      An inline function in C++ is one of the most important features of C++.

Inline Function in C++

so When the program starts executing the function first of all call instruction, the CPU stores the memory address of the instruction follow the function call, copy the arguments of the function on the stack and finally transfers control to the specified function. so

The RAM then execute the function code, stores the function return value in a predefined memory location and then returns control to the calling function.

Remember one thing that inline function is only a request which is done by the programmer or program to the C++ compiler to proceed that work which was told or written in the program to do to the compiler by using an inline function. so

It means that if C++ compiler wants to ignore the request the compiler can ignore that request which is done by the compiler to do the work which is told by the program to do.

The compiler can ignore that request because the inline function is not a command it is just a request that’s why the compiler can ignore the request of inlining.

C++ compiler does not accept inlining request in these circumstances:-

1. The function having the loop. Compiler check that the function is having loop or not. Because If the function contains loop(for, while do-while) then the compiler will not accept the request of inlining.

2. The function contains static variables. ( Because if Any function which contains static variable the compiler will not accept the request of inlining).

3. The function is Recursive. ( Because If the function is recursive in that case the compiler will not accept the request of inlining).

4. The compiler will check function return type is other than void or not and the return statement does not exist in the function body. Because If it then the compiler will not perform inlining in this case.

5. The Function should not contain switch and goto statement. Because If the function contains a switch or goto statement then the compiler will not execute the program using an inline function in C++.

Now let’s come to the example of the inline function in C++

 

Write a program to calculate factorial of a number using the inline function in C++.

                                         INPUT 

#include<iostream.h>
#include<conio.h>void main()
{
int n;
clrscr();
cout<<“Enter a number to calculate factorial”;
cin>>n;
fact(n);
getch();
}
inline void fact(int n);
inline void fact(int n)
{
int f=1,i;
for(i=1;i<n;i++)
{
f=f*i;
}
cout<<“factorial of “<<n<<“is = “<<f;
}

                                         OUTPUT

Enter a number to calculate factorial
5
factorial of 5 is = 120
Write a program using the inline function in C++.

                                INPUT

#include <iostream>
#include<conio.h>
class example
{
int a,b,add,sub,mul;
float div;
public:
void get();
void sum();
void difference();
void product();
void division();
};
inline void example :: get()
{
cout << “Enter first number:”;
cin >> a;
cout << “Enter second number:”;
cin >> b;
}
inline void example :: sum()
{
add = a+b;
cout << “Addition of two numbers: ” << a+b << “\n”;
}inline void example :: difference()
{
sub = a-b;
cout << “Difference of two numbers: ” << a-b << “\n”;
}inline void example :: product()
{
mul = a*b;
cout << “Product of two numbers: ” << a*b << “\n”;
}inline void example ::division()
{
div=a/b;
cout<<“Division of two numbers: “<<a/b<<“\n” ;
}int main()
{
cout << “Program using inline function\n”;
example a;
a.get();
a.sum();
a.difference();
a.product();
a.division();
getch();
}

                                  OUTPUT

Enter first number: 30
Enter second number: 30
Addition of two numbers: 60
Difference of two numbers: 30
Product of two numbers: 900
Division of two numbers: 1

 

First of all, we should know that in inline the definition part is copied into calling function because of this inline function does not require for function calling overhead.

During Function calling inline function saves the overhead of variables on to the stack.

Hope you got your all the answers regarding inline function in C++. The inline function is a way to define the function to pass the control from called to calling because in simple function always the control passes from calling function to called function.

Now lets began our next and important topic Extern Function in C++:

Extern function in C++

This Function has to be accessed by some other file. Extern keyword is used to declare extern function. 

Extern Function is accessed by using

 

                                #include”file.cpp”  

where file.cpp means file name which you want to access by using that program.

Syntax For declaring Extern Function in c++ is

      extern_function function_name(parameter list);

In simple language, So we can say that Extern function in C++ is a function which is used to access any other program by the own program which you are writing. You can access any other program in a single program by using just the name of the program which you want to access.Due to Extern Keyword C++ programs are esay and suitable for large programs.

The extern function is very important while writing very large or big program. When you write a big program you may not able to find the error easily but if you make the parts of the single large program you may be easily able to get and correct the error of that program.

Extern Function is very helpful like as same as the inline function in C++.

Examples Of Extern Functions in C++

                            INPUT

                       first.cpp                                  second.cpp
void sum()
{
int a,b,total;
cout<<“Enter two numbers\n”;
cin>>a>>b;
total=a+b;
cout<<“Total = “<<total;
}
#include<iostream.h>
#include<conio.h>
#include”first.cpp”
extern void sum();
void main()
{
clrscr();
sum();
getch();
}

             OUTPUT

Enter two numbers
5
11
total = 16

Extern Function 

As you can see the given example in above is programmed by using Extern Function. Here you can see there is two program created in the different file. One file name is “First.cpp” and the second one name is “second.cpp”. To take the value we have programmed a file in first.cpp and accessing that data from the second.cpp.

This is very helpful because we have saved this and whenever we have to use these data we can easily access by the extern function which is given in c++.

 

Hope You have got your all the answer regarding inline function in C++ and extern function in C++. Whereas much as possible to me I have tried to give you the best. I have explained to you by using examples and theory. We have discussed first of all inline function in C++ then after another important topic Extern function in c++.

Thanks for reading article inline function in c++. If you liked it please comment your questions. If you don’t like this article please give suggestions to improve so that it will help me to give you the best quality. Because your suggestion will help me a lot to improve the quantity provided in my website.

Function overloading in c++

Speak Your Mind

*