One solution is to overload functions, as other answers have said.
Another solution is to use boost::optional for an optional argument:
int f2( int n1, boost::optional<int> n2) { int n2value = n2 != boost::none? n2.get() : f1(n1);
boost::optional usually helps when you have a few optional arguments, for example:
int f(int a, boost::optional<X> b, boost::optional<Y> c, boost::optional<Z> d) {
In such a situation, the overload function explodes, as the number of functions increases linearly with each additional optional parameter. Fortunately, C ++ does not have a named parameter, otherwise it will increase exponentially, not linearly. :-)
Nawaz source share