More

    Inline Function in C++.

    A function is a set of instructions that are called by its name in another function. The function in which the function is called is called the calling function.

    When the function is called, the control of the program is transferred from the calling function to the body of the function to be called and after the call is completed, the control of the program is again transferred to the calling function. comes) and the program flow proceeds, so while calling any function, the following actions are completed –

    1. The status of the new register is stored in a stack.
    2. Argument is passed to the called function.
    3. After the work is done, the register is brought back to the status quo.
    4. The result is returned by the called function.

    What is Inline Function?

    The above procedure of calling a function is difficult from the point of view of the program because in this the control of the program is transferred from one place to another place, if a large number of functions are used in the program. Then the program execution increases. To overcome this complexity of function calling, inline functions are used in C++.

    Inline function works like a macro. In which the function can replace the entire text of the body in the Compiler Main Program.

    also fall

    Inline function can be declared in the following way.

    return_type function_name (Argument_list)
    {
    Function_Statements ;
    ——————————
    ——————————-
    }

    Important Notes

    • Inline functions are declared before the function is called.
    • The inline function can also be overloaded.

    Example of Inline Function

    # include
    # include
    inline int squre (int a)
    {
    return (a * a) ;
    }

    void main ( )
    {
    clrscr() ;
    int x ;
    cout << “n enter any no. =” ;
    cin >> x ;
    cout << “n square of input no is =” << square(x);
    }

    Output :-

    Enter any no = 7
    Square of input no. is = 49

    The inline function has its own limitations, if the inline function is very large, then the compiler can also expect it, because no command is given to the compiler through the inline keyword, but requests are made to it. Considering the status of the compiler accepts this request. Inline function, along with making program execution fast, also takes up a lot of space in memory.

    Default Argument in Inline Function

    While calling a function in C++, the function can be called without fixing the Argument value. In this case, the function fixes a value in the arguments in the form of By Default which is similar to the argument given at the time of call of the function. The default value of the Function Argument is assigned at the time of Function Declaration. Defaults are always assigned from the left side only.

    The following example shows the default Argument function –

    #include

    #include

    inline int add (int a, int b = 9, int b = 9, int c =25)
    {
    Return (a+b+c);
    }

    void main()
    {
    clrscr() ;
    Int x = 12 ;
    cout << “n add =” << add(x);
    }

    Output :-

    46

    In this the int add (int a, int b= 9, int c=25) function defaults to 9 and 25 values ​​for the prototype arguments b and c respectively. When the add function is called in the main function. For example, add (x) then in the add function the value of x is found in a . And the value of the other two arguments are found by default. So the output is 12 + 9 + 25 = add = 46.

    Note: – In the default argument function, the value of the argument is assigned from right to left.

    inline int add (int a = 12, int b, int c) wrong declaration
    inline int add (int a, int b = 6, int c) wrong declaration
    inline int add (int a, int b, int c = 6) Correct declaration

     

    Recent Articles

    Related Stories

    Leave A Reply

    Please enter your comment!
    Please enter your name here

    Stay on op - Ge the daily news in your inbox