Easy tutorial
for C / C ++ Function 4 (Default
argument or default parameter)
Default argument (default parameter)
- When calling a function, it is usually called according
to the number of arguments. However, you can also call it with a different
number of arguments using the default parameter.
- Let's look at an example.
Example Code
#include <iostream>
using namespace std;
void defaultP(int a = 3, int b = 30, int c = 300);
int main() {
cout << endl;
cout
<< "##############################" << endl;
cout << " Default
parameter " <<
endl;
cout
<< "##############################" << endl;
cout
<< endl;
cout
<< " No parameters " << endl ;
defaultP();
cout << " 1 parameter "
<< endl ;
defaultP(5);
cout <<
" 2 parameters " << endl ;
defaultP(5, 50);
cout << " 3 parameters "
<< endl ;
defaultP(5, 50, 500);
return 0;
}
void defaultP(int a, int b, int c){
cout
<< " a = " << a << ", b = " << b
<< ", c = " << c << endl;
cout
<< endl;
}
- We defined function
'defaultP' with three integer arguments and no return value.
- In the declaration section of
the function, the integer variables 'a', 'b' and 'c' are given default values
of 3, 30, and 300, respectively.
- In the definition part of the
function, there is print statement of 'a', 'b' and 'c' values received as
arguments.
- The 'defaultP()' function was
called after the output statement "No parameters".
- We will then call the
function by incrementing the arguments one by one. At the end, I called the
function 'defaultP(5, 50, 500)' after the output statement called "3
parameters".
- Let's check the results.
results :
##############################
Default parameter
##############################
No parameters
a = 3, b =
30, c = 300
1 parameter
a = 5, b =
30, c = 300
2 parameters
a = 5, b =
50, c = 300
3 parameters
a = 5, b =
50, c = 500
- " Default parameter "
will be displayed first.
(1) "No parameters": Because the function without arguments ( defaultP()
) is called, you can see that the default values are printed.
(2) "1 parameter": Because
the function with one argument is called ( defaultP(5) ), the value of the
first argument 'a' is changed to 5 and it is printed. At this time, the
remaining values are printed to the default values. ("b = 30, c = 300")
(3) "2 parameters": Because
the function with two arguments ( defaultP(5, 50) ), you can see that the value
of the first argument 'a' and 'b' is changed to 5 and 50, respectively. The value
of the remaining 'c' is set to 300, which is set to the default value.
(4) "3 parameters": Because
the function with three parameters ( defaultP(5, 50, 500) ) is called, you can see
that it is printed as input value instead of default value.
Note:
1. The value of the default
parameter is specified in the declaration part of the function.
2. Give all the default values
in the function arguments part, or input sequentially from right to left.
ex) void test(int a=1, int b=2, int c=3); (O)
void test(int
a, int b=2, int c=3); (O)
void test(int
a=1, int b, int c); (X)
void test(int
a, int b=2, int c); (X)
No comments:
Post a Comment