C++27: Inline Functions

  1. Whenever a C++ Program is compiled the compiler produces a unique address for the compile code of each function.
  2. The Regular functions are called by jumping on the function address, execute the function code and come back to calling the function.
  3. This process of calling functions by jumping on function address takes some and due to this program takes.
  4. Inline functions are the functions for which compiler replaces the function call with the corresponding function's compile code and due to this there is noo neeed to call the function by jumping on the function address and program takes less time for execution.
  5. We can define a function as inline by using the keyword inline as prefix to the function prototype.
NOTE:
  1. If the function is inside the class declration such function is automatically a inline function.(if it is small not complicated)
  2. Declaring function inline means requesting the compiler to treat the function as inline.
  3. If the function is not complicated and small then the function will be treated as inline function otherwise the function will be treated as regular function.


Example: Regular function calling in cpp with their address. In this example the program control will jump to sum function and then execute it. This takes time.

#include <iostream>
using namespace std;
int sum(int a, int b)
{
    return (a + b);
}
int main()
{

    cout << "Address of sum function = " << (int *)&sum << endl;
    int (*p)(intint);
    p = sum;
    cout << "p = " << (int *)p << endl;
    int x = (*p)(1020);
    cout << "x = " << x << endl;
}

Output:

Address of sum function = 0x4015504
p = 0x401550ce in Inches is : 72
x = 30


Example Inline Function:
In the we declare the circumference function as inline function and the program control will not jump to circumference as the copy of comlie code of circumference is present in the prototype. This takes less time. 

#include <iostream>
using namespace std;
class Circle
{
private:
    int R;

public:
    inline void setradius(int r);
    inline void area();
    inline void circumference();//declaring function inline
};
void Circle::setradius(int r)
{

    R = r;
}

void Circle::circumference()
{
    float circum = 2 * 3.14 * R;

    cout << "Circumference of circle is " << circum << endl;
}
void Circle::area()
{
    float area = 3.14 * R * R;

    cout << "Area of circle is " << area << endl;
}
int main()
{
    Circle c1;
    c1.setradius(2);
    c1.circumference();
    c1.area();
}

Output:

Circumference of circle is 12.56
Area of circle is 12.56

Example 2:

#include <iostream>
using namespace std;
class Distance
{
private:
    int I;
    int F;

public:
    void setdata(int f, int i)
    {
        F = f;
        I = i;
    }

    inline void showdata();
    int convert();
};

void Distance::showdata()
{
    cout << "Distance is " << F << " feet and " << I << " Inches" << endl;
}
int Distance::convert()
{
    int inches = I + (F * 12);

    return inches;
}
main()
{
    int in;
    Distance a;
    a.setdata(224);
    a.showdata();
    in = a.convert();

    cout << "Total distance in inches is: " << in << endl;
    return 0;
}

Output:

Distance is 2 feet and 24 Inches
Total distance in inches is: 48













Comments

Popular posts from this blog

C++ 38: Visibility Modes Public, Private and Protected