Example 1:
To make a code in C++, we must insert the header files that aid in using the function in our code. The header files that we need in this code are the “iostream” and the “string” header files. The “iostream” helps in taking the input or displaying the output data since the “cin” or “cout” function is declared in it. On the other hand, “string” is included here as it aids us while working with string data. Then, we use the “namespace std”.
After this, we declare a function with the “myNewFunc” name and pass a parameter which is the default parameter of this function. We initialize a “string” here with the “fruit_name” name and adjust its value to “WaterMelon”. In this function, we use the “cout” where we print the “fruit_name”.
Now, we place the driver code, which is the “main()” function, in the C++ code and call the function that we previously declared inside it. First, we call this function by passing any parameter. In the next line, we pass the parameter to the function which is “Apple”. Below this, we place “Pear” as the parameter of the “myNewFunc”. Then, in the line ahead, we call this function without passing any parameter. So, in the same way, we call this function multiple times in our code by passing the parameters and without passing the parameters.
Code 1:
#include <string>
using namespace std;
void myNewFunc(string fruits_name = "WaterMelon") {
cout << fruits_name << endl;
}
int main() {
myNewFunc();
myNewFunc("Apple");
myNewFunc("Pear");
myNewFunc();
myNewFunc("Mango");
myNewFunc();
return 0;
}
Output:
This output renders that the default parameter value is added when we call the function without adding any argument or parameters. We can see that “WaterMelon”, which is the default parameter of this function, appeared three times which means that we called the function three times without passing any argument.
Example 2:
Here, we declare a function by passing four parameters. The function that we created here is the “sum” function with the “int” type. The four parameters that we added here are “num_1”, “num_2”, “num_3”, and “num_4”. The data type of all four parameters is “int”. We also initialize “num_2”, “num_3”, and “num_4” with the values of “4”, “9”, and “2”, respectively.
Then, we utilize the “return” function and insert all four parameters by placing the “+” sign after each parameter so it returns the sum of all four parameters of the function. Now, we move ahead and call the “main()” function. First, we add only one parameter to the function that we previously created and then we add two. Below this, we add three and four parameters to the functions. Since we declared the function with four parameters, it gets the default values here from the function that we added during the function declaration.
Code 2:
using namespace std;
int sum(int num_1, int num_2= 4, int num_3 = 9, int num_4 = 2)
{
return (num_1 + num_2 + num_3 + num_4);
}
int main()
{
cout << sum(40 ) << endl;
cout << sum(40, 25) << endl;
cout << sum(11, 19, 20) << endl;
cout << sum(10, 15, 25, 30) << endl;
return 0;
}
Output:
Here, it shows “55” as the result when we call the function. But in our code, we just pass only one parameter which is “40”. So, it takes the remaining three parameters as the default parameters and calculates the result accordingly. The same is true for the remaining two results, but the last result didn’t get the default value since we passed all four parameters to the function while calling that function.
Example 3:
The function which we declare in this example is the “proFunction()”. The parameter that we pass here is the “nameOfProf” with the “string” data type and adjust its value as “James”. This is the default argument of this function. Then, we print it using “cout” in this function. So, the “nameOfProf” is printed when we call this function.
Now, we add “int main()” below this function and then call “proFunction()” without any parameter. Here, it gets the default parameter. Below this, we pass “Peter” as the parameter of the function. Then, we call this function again without a function parameter. In the next line, we add “William” as the function’s parameter and call the function again without a parameter below this. After this, we use “return 0” here.
Code 3:
#include <string>
using namespace std;
void proFunction(string nameOfProf = "James") {
cout << " Professor name is " << nameOfProf << endl ;
}
int main() {
proFunction();
proFunction("Peter");
proFunction();
proFunction("William");
proFunction();
return 0;
}
Output:
The following result shows that the default parameter value is included in the function when it is called without any argument or parameter. The fact that the function’s default parameter, “James”, appears three times indicates that we invoked the function three times without providing an argument.
Example 4:
The “charDisplay()” function with two parameters is declared in this code. The first parameter here is “char” which is initialized with “*” and the other is “int” which is initialized with “5”. Now, we have the “main()” function where we initialize the “num_count” of the “int” data type and adjust “6” here as its value.
Now, we print the “Default parameter” line and then call the function below this by passing any parameter. Here, the default parameters, which are “*” and “5,” are utilized. After this, we print “The argument we passed” using “cout” and then add “@” as the parameter. Here, “@” and “5” are the parameters.
Now, we call this function again without parameters and then call it by passing “$, num_count”. Here, the parameters are “$” and “6” since the value of “num_count” is “6”. Now, we define the default parameter where we insert the “char” variable name “my_ch” and the “int” variable name “num_count”. In this, we utilize the “for” loop and then “cout” the “my_ch”.
Code 4:
using namespace std;
void charDisplay(char = '*', int = 5);
int main() {
int num_count = 6;
cout << "Default parameter: ";
charDisplay();
cout << "The argument we passed: ";
charDisplay('@');
cout << "Default parameter: ";
charDisplay();
cout << "The argument we passed: ";
charDisplay('$', num_count);
return 0;
}
void charDisplay(char my_ch, int num_count) {
for(int i = 1; i <= num_count; ++i)
{
cout << my_ch;
}
cout << endl;
}
Output:
Here in the first three lines, the characters appear five times. But in the fourth line, the “$” character appears six times as we passed “num_count” as the second parameter. The value of this “num_count” is “6”. The default parameter appeared in the first and third lines because we didn’t pass the parameters while calling the function in our code.
Conclusion
The “default parameters to function” in C++ programming are discussed in this tutorial. We explored how the function gets its default value when we call it without passing any parameter. The default arguments are adjusted while declaring the function. We discussed that the default parameters are only utilized when we didn’t pass any parameter to our function while calling it. We showed the codes here where we utilized the default parameters to function in our C++ code.