# Inline Functions, Default Arguments & Constant Arguments in C++

38
0

## Inline Functions

In C++, an inline function is a function that is expanded in-line at the point where it is called, rather than being executed through a function call. The main advantage of using inline functions is that they can improve the performance of your program by reducing the overhead of function calls.

To define an inline function, you need to use the inline keyword before the function definition. For example:

inline int add(int a, int b) {
return a + b;
}
C++

When the add function is called, the compiler will replace the function call with the actual code of the function. This can result in faster code execution, but it can also increase the size of the executable file.

It’s important to note that the inline keyword is only a suggestion to the compiler, and it’s up to the compiler to decide whether or not to actually inline the function. Generally, smaller functions that are called frequently are good candidates for inlining.

## Default Arguments

In C++, you can provide default arguments for function parameters. Default arguments are values that are used if no value is provided for a particular parameter when the function is called.

To provide a default argument, you need to specify it in the function declaration. For example:

void print(int x = 0, int y = 0) {
cout << "x = " << x << ", y = " << y << endl;
}
C++

In this example, the print function has two integer parameters with default values of 0. If the function is called without providing values for these parameters, the default values will be used. For example:

print();        // prints "x = 0, y = 0"
print(1);       // prints "x = 1, y = 0"
print(2, 3);    // prints "x = 2, y = 3"
C++

Note that default arguments can only be provided for the rightmost parameters in a function declaration. For example, the following code is not valid:

void print(int x = 0, int y) {
cout << "x = " << x << ", y = " << y << endl;
}
C++

## Constant Arguments

In C++, you can specify that a function parameter should not be modified by the function. This is done by using the const keyword before the parameter type. For example:

void print(const string& s) {
cout << s << endl;
}
C++

In this example, the s parameter is a reference to a string that cannot be modified by the print function. This helps to ensure that the function does not accidentally modify the value of the string.

You can also use the const keyword to specify that a member function does not modify the state of an object. This is done by using the const keyword after the function declaration. For example:

class MyClass {
public:
int getValue() const {
return value;
}
private:
int value;
};
C++

In this example, the getValue function is a const member function that does not modify the state of the MyClass object. This allows you to call the function on const objects, like this:

const MyClass obj;
int x = obj.getValue();
C++
WRITTEN BY

## xalgord

Constantly learning & adapting to new technologies. Passionate about solving complex problems with code. #programming #softwareengineering