Thursday, January 31, 2019

Inline function in c++ programming

in this post, you will learn about the inline function in c++ programming and learn how to use an inline function in programming.

inline function in c++ programming, learn c++ programming
learn c++ programming

Inline function: 

A function which can be expended in line is called an inline function. it's help in reducing the function call overhead.

to make a function inline you just need to add the inline keyword at the beginning of the definition of the function.

Example: 
                inline int squre(int a)
                {

                  return a*a;
                 }

Note: in the inline function, there is only one line of code. like you see in the above example.

the inline function advises the compiler to generate a copy of the function's body code in every place where the function is called.
typically inline function placed in the header section of the program.

Example c++ program using an inline function 

              #include <iostream>

              inline squre(int a)
              {
                return a*a;     // inline function is alway one line of code in body section.
              }

              int main()
              {

                int x;
                std::cout<<"\n enter the value of x:";
                std::cin>>x;

                std::cout<<squre(x);

                return 0;

               }

so, I think now you understand what is the inline function, and how to use it in programming.

"please share this knowledge as much as possible as you can and also comment me your questions and queries under the comment section, it's really grateful to me to help you."


0 comments: