Functional concept

void execute(int &x,int y=100)
{
 x=x+y;
 cout<<x<<endl;
}
void main()
{
 int a=5,b=6;
 execute(b);
}

the following program will work without assigning a default value of x (formal parameters in the function prototype).

+3
source share
4 answers

Yes it will work. By not setting a default value x, you force the caller to pass the value as a parameter. When you execute(b)mostly do the execution, you link the link xto the actual parameter "b", and since you did not pass any value to the variable "y", the default value will be used.

+4
source

-, ++ , " ". "Prototype" - C, ++. , .

-, , , ++, ? , - , .

-, int main, void main.

+3

execute(b), , execute b = b + 100 (y 100, , ), 106 b main, .

+1

I think you are confused about how the function works. You pass the value of x to it (well, a reference to a intnon-actual value), so ... yes, that works.

By providing a default value for y ( int y=100), you make sure that this function can be called without passing a second argument to it. If called with a single argument, y will be set to 100.

int a=5,b=6;
execute(b);

Inside execute (), the initial value of x is 6, and y is 100.

0
source

Source: https://habr.com/ru/post/1741883/


All Articles