C++

C++ Default Parameters

A function is a code declared outside the main function but executed when called from the main program through a function call. Any value passed to any function through the function call is known as the parameter of the function. These arguments are variables of specific data types. These parameters can be default or introduced at run-time.

Default Parameters

A default argument or parameter is the value introduced to the function at the time of function declaration. This value is assigned by the compiler automatically when the function is called. During any function call, if no value is passed as a parameter to the function, then it means that the default value will be utilized. Otherwise, the default value will be replaced with the new one.

Implementation of Default Parameters

Example 1

Here, we will quote a sample example. This example will contain a function to calculate the sum of the values passed as parameters by the function call. The function will contain four variables. First, all are initialized as zero. We will call the function in the main program by passing arguments to the function. The number of arguments may vary. We will pass two arguments, then three, and four parameters will be passed in the last function call.

Sum (20, 25, 30, 35);

When you pass two parameters, these two will be stored in only two variables; while the rest will remain zero. A similar case is for the function call with three parameters. But, when there are four arguments, then it means that all four variables will contain values to be added together.

Save the code and then, execute them in the Ubuntu terminal by using a g++ compiler.

$ g++ -o def def.c

$ ./def

You will see the resultant value for each function call.

Example 2

This example deals with using default parameters differently for each function call, as explained in the previous example. First, define the function with the return type and the default parameters. As we know, the default parameters are declared at the start. So, we will declare the values for the two variables here:

Void display(char = '*' , int = 3);

The display is the name of the function. This default declaration of parameters will work when the function is called with empty arguments brackets or with no value. Inside the main program, we have declared a count variable of integer type, which will be used in the loop for iteration up to this limit. First, the function call is made with no arguments inside it. That means the default parameters declared for the function will be used. So the parameters for this function call will be * and 3. In the second function call, we have passed a single parameter symbol “#”. So, in this case, the default parameter of the second variable, 3, will be used as it is. But the symbol that is passed will be replaced by the symbol we have declared earlier.

Display ('#');

Now, coming toward the third function call, two arguments will be passed this time, a symbol ‘$’, and the integer variable as count, which was declared as 5 in the main program. Both the default parameters will not be used; the new arguments will replace these default parameters.

Display ('$', count);

Outside the main function, the display function, which was first declared, is now executed by having a for loop that will iterate till the count value. Every iteration will display the character passed to it. Otherwise, the default value will be displayed.

Now, execute the whole code. You will see that each time a symbol is displayed at the nth time, the value is passed as a parameter.

Example 3

This example will be about the function overloading phenomenon in C++ while having default parameters.

Function overloading

For example, a function with a name sample is described as:

Int sample (int a) {}

Int sample (int a, int b) {}

These described functions have the same name but have different arguments. Now, we will consider a C++ program to elaborate this concept in default parameters.

In the main function, the function is called with a different number of parameters, with 2, 3, and 4 values. Now, execute the code, you will see that an error of ambiguity has occurred.

When two functions of the same name are declared, it is necessary to have different parameters. But both functions having the same name contain almost the same parameters, two same and two different. The same arguments of the function will confuse the compiler while choosing which function to choose because the specifications of both the functions are the same. We have a solution to solve this dilemma of ambiguity regarding default parameters.

First, we will change the return type of both functions. Similarly, the parameters will have consistency in the variable’s data type. For example, one function will take the same data type for all the variables, either integer or float.

Float sum ( float x, float y, float z, float w);

Now, in the main program, while calling the function, we will take both the values according to the data types. But remember that all the numbers as arguments will follow the data type of the declared variable. In other words, all the numbers in a single function call will be of the same data type.

Sum (1.0f, 1.5f, 2.5f, 3.5f);

We have mentioned “f” in a short form with each float value to identify that it is a float value and will be passed to the function having float data type variables.

On execution, you will see the obtained results; first and third values will be obtained from the function with integer data types. While the second will be obtained from the function having float data types.

Example 4

This example is an overloading function with a difference in the number of parameters. A function display is declared here having no return type, as the cout statement inside the function will display the results. One function will take two parameters: int and a double data type. The second function will take a single variable of double data type, and the third one will take only an integer data type variable.

Two variables will be declared in the main program. These values will be passed to the function. The function call is for the specific function in each cell depending on the number of default parameters.

Conclusion

Default parameters are declared within the function at the time of declaration of a function. We have used some examples in C++ to elaborate on the declaration and process of default parameters in the Ubuntu system. During function overriding, using default parameters can be a trouble-making situation with the same default parameters. We hope you found this article helpful. Check the other Linux Hint articles for more tips and tutorials.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.