C/C++

Inline Function In C++ With Example

Inline function:

inline function- By using function the size of the program is reduced. It is because a defined function can be called at multiple places in a program by using only a single call statement. When the compiler compiles the program, it generates a special code at the function call to jump to the function definition. It also generates another special code in the function definition (usually at the end of a function definition) to transfer back the control to the calling function. Thus, extra execution time is spent during the execution of the program to jump the control from the calling function to function definition and then return back to the calling function.

The code of the body of the function can be inserted at each place of the function call to save the jumping time during program execution. The inline function is used for this purpose.

The inline function is a special function. It is a user-defined function but in it the function prototype is omitted. The keyword “inline” is used in the declaratory of the function definition. The inline function is defined before the main() function.

The actual code of the body of inline functions is inserted at the place f each function call during the compilation. If the inline function is called ten times in the program then the same code of the function will be inserted at each place of the function call. In this case, multiple copies of the same function code are inserted in the program and thus making the size of the program larger. inline is used only if the size of the function is small.



Examples:

The following program example finds out the cube of a number using the inline function:

#include <iostream>

using namespace std;

inline long cube(int x)
{
    return x*x*x;
    
}
int main()
{
   int n;
   cout<<" enter any integer value: ";
   cin>>n;
   cout<<" Cube of "<<n<<" is: "<<cube(n);
}

inline function

Example: write a program by defining a function “kg” to convert pounds into kilograms:

1pound= 0.453592

#include <iostream>

using namespace std;

inline float kg(float p)
{
    return p*0.453592;
}
int main()
{
   float pounds;
   cout<<" enter weight in pounds: ";
   cin>>pounds;
   cout<<" "<<pounds<<" pounds = "<<kg(pounds)<<"kilograms";
}

inline function


Example: write a program by defining an inline function “circle_area” that takes the radius of a circle and returns the calculated area of the circle:

Area of circle = R2  and value of  is 3.1417

#include <iostream>

using namespace std;

inline float circle_area(float r)
{
    return 3.1417*r*r;
}
int main()
{
   float radius;
   cout<<" enter radius of circle: ";
   cin>>radius;
   cout<<" Area of circle = "<<circle_area(radius);
}

inline function

Example: write a program by defining a function “power” to find and return the exponential power, i.e. baseexponent:

For example, if base =2 and exponent = 3 then the function should return 8

#include <iostream>

using namespace std;

inline long power(int b, int p)
{
    long i, res=1;
    for(i=1; i<=p; i++)
    res=res*b;
    return res;
}
int main()
{
   int base, exponent;
   cout<<" enter base value: ";
   cin>>base;
   cout<<" enter exponential value: ";
   cin>>exponent;
   cout<<" Result is: "<<power(base, exponent);
}

inline function


Related Article:

https://programmingdigest.com/function-overloading-in-c-with-examples/

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button