Computer Science - Object Oriented Programming Using C++ - Discussion
Discussion Forum : Object Oriented Programming Using C++ - Section 3 (Q.No. 9)
9.
If you assign a default value to any variable in a function prototype's parameter list, then _____
Discussion:
3 comments Page 1 of 1.
Eklesh parmar said:
8 years ago
Ex.
int add(int a, int b, int c= 50).
Default argument are declared from righ- hand side.
Call add(10,20)
a=10, b=20 and c=50 is default value.
int add(int a, int b, int c= 50).
Default argument are declared from righ- hand side.
Call add(10,20)
a=10, b=20 and c=50 is default value.
Ayesha said:
4 years ago
void printValues(int x, int y=10) // 10 is the default argument, y is now an optional parameter
{
std::cout << "x: " << x << '\n';
std::cout << "y: " << y << '\n';
}
int main()
{
printValues(1); // y will use default argument 10
printValues(3, 4); // y will use user-supplied argument 4
}
This program produces the following output:
x: 1
y: 10
x: 3
y: 4
{
std::cout << "x: " << x << '\n';
std::cout << "y: " << y << '\n';
}
int main()
{
printValues(1); // y will use default argument 10
printValues(3, 4); // y will use user-supplied argument 4
}
This program produces the following output:
x: 1
y: 10
x: 3
y: 4
SABA said:
4 years ago
@All.
If you want to override constructor default values for an object you are instantiating, you must also override.
B. all parameters to the left of that value.
Things to Remember:-
Once we provide a default value for a parameter, all subsequent parameters must also have default values.
For example:
// Invalid
void add(int a, int b = 3, int c, int d);
// Invalid
void add(int a, int b = 3, int c, int d = 4);
// Valid
void add(int a, int c, int b = 3, int d = 4);
If you want to override constructor default values for an object you are instantiating, you must also override.
B. all parameters to the left of that value.
Things to Remember:-
Once we provide a default value for a parameter, all subsequent parameters must also have default values.
For example:
// Invalid
void add(int a, int b = 3, int c, int d);
// Invalid
void add(int a, int b = 3, int c, int d = 4);
// Valid
void add(int a, int c, int b = 3, int d = 4);
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers