C++Tutorials

Inline Function And Default Argument in C++

In this article, we will learn about the inline function and default argument in c++.

What is Inline Function?

The inline function is written like a normal function in the source file but compiles into inline code instead of into a function.

Calling a function generally causes a certain overhead (stacking arguments, jumps, etc…), and thus for very short functions, it may be more efficient to simply insert the code of the function where it is called, instead of performing the process of formally calling a function.

When a function is declared with inline specifier,   the function body is actually inserted into the program wherever a function call occurs during compile time. Also, the source file remains well organized and easy to read, since the function is shown as a separate entity.

Declaring a function as inline does not change at all the behavior of a function, but is merely used to suggest the compiler that the code generated by the function body shall be inserted at each point the function is called, instead of being invoked with a regular function call.

Functions that are very short, say one or two statements, are candidates to be inlined.

Syntax of inline function:

A function is made inline by adding keyword inline at begining of its definition.

inline return_type function_name(args...) 
{
       //function body
}

Here’s an example program that uses inline function.

// demonstrates inline functions
#include <iostream>
using namespace std;

// converts mile to kilometer
inline float mile_to_km(float miles)
{
    return 1.6093 * miles;
}
//--------------------------------------------------------------
int main()
{
    float miles;
    cout << "Enter length in mile: ";
    cin >> miles;
    cout <<endl << "Equivalent length in km : " << mile_to_km(miles);
    return 0;
}

The output of the program is:

Enter length in mile : 12 
Equvalent length in km : 19.3116

Note: The inline keyword is actually just a request to the compiler. So, there may be a case where the compiler ignores the request and compile the function as a normal function. For instance, it might decide based on length of the function.

What is default argument ?

In C++, functions can also have optional parameters for which no arguments are required in the call. In other words, a function can be called without specifying all its arguments. For example, a function with three parameters may be called with only two.

For this, the function shall include a default value for its last parameter, which is used by the function when called with fewer arguments. 

For example:

// demonstrates default arguements
#include <iostream>
using namespace std;

// multiply two integers
int multiply(int multiplicant,int multiplier=2);            //declaration with default argument
//--------------------------------------------------------------
int main()
{
    cout << "multiply(10) = "<<multiply(10) << endl;        //prints 20
    cout << "multiply(10,20) = "<<multiply(10,20) << endl;  //prints 200
    return 0;
}

int multiply(int multiplicant,int multiplier)               //default value supplied if necessary
{
    return multiplicant * multiplier;
}

The output of the program is:

multiply(10) = 20 
multiply(10,20) = 200

Here, in the first call multiply(10) only one argument to the function, even though the function has two parameters. In this case, the function assumes the second parameter to be 2. Therefore, the result is 20.

However, in the second call multiply(10,20) two arguments are passed to the function because of which, the default value for multiplier = 2 is ignored. Therefore, the result is 200.

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

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

Back to top button