+ 1
Does default function always come in ()?
Default Arguments
3 ответов
+ 2
I'm not understanding your question. Can you pass me an example code of your doubt? your title mentioned 'default function', but in Description you put 'Default arguments', which is the question topic actually?
0
What is the output of the following code?
void printSum(int a, int b = 4)
{
cout << a + b << endl;
}
int main()
{
printSum(13);
}
this is a function and its answer is 17 . i did not understand logic.... Please explain me.
- 1
The `printSum` function is declared to accept two arguments. One is mandatory <a>, and one is optional <b>, with a default value provided for <b> (4).
When we call a function that has default parameters we can skip the optional ones. The optional arguments that we skip will be assigned the default value as per specified in function declaration.
So in your case, `printSum(13)` we only pass a value for <a>, and skip passing a value for <b>. Therefore <b> is assigned the default value (4), and then output the sum of <a>(13) + <b>(4) which gives us 17.