C++ | Function Overloading and Default Arguments | Question 2
Output?
#include<iostream> using namespace std; int fun(int x = 0, int y = 0, int z) { return (x + y + z); } int main() { cout << fun(10); return 0; } |
chevron_right
filter_none
(A) 10
(B) 0
(C) 20
(D) Compiler Error
Answer: (D)
Explanation: All default arguments must be the rightmost arguments. The following program works fine and produces 10 as output.
#include <iostream>
using namespace std;
int fun(int x, int y = 0, int z = 0)
{ return (x + y + z); }
int main()
{
cout << fun(10);
return 0;
}
Recommended Posts:
- C++ | Function Overloading and Default Arguments | Question 5
- C++ | Function Overloading and Default Arguments | Question 3
- C++ | Function Overloading and Default Arguments | Question 4
- C++ | Function Overloading and Default Arguments | Question 5
- Default arguments and virtual function
- Templates and Default Arguments
- Some Interesting facts about default arguments in C++
- getopt() function in C to parse command line arguments
- Does C support function overloading?
- Function Overloading and float in C++
- Function overloading and const keyword
- C++ | Operator Overloading | Question 5
- C++ | Operator Overloading | Question 6
- C++ | Operator Overloading | Question 7
- C++ | Operator Overloading | Question 10



