+ 1
How to use a function with 2 parameters, using the default value of the first parameter.
Suppose we declare a function containing 2 parameters, and we set a default value to the first parameter. So while calling the function, what code do I write in order to use the deafult value of the first parameter while providing a value for the second parameter.
4 Answers
+ 6
Here in js.
var i = 4;
function h(a,d = a+2) {
alert(d);
}
h(i);
+ 2
I think the default parameter is set only for the later parameters.
I mean we cannot set default parameters from left to right.
eg.
void func(int a, int b=5, int c=2){} is ok,
but
void func(int a=3, int b=5, int c){} is invalid.
+ 2
in c++ default arguments have to be trailing: https://en.cppreference.com/w/cpp/language/default_arguments
most other languages also have trailing function arguments and/or named arguments
+ 1
why don't you put default value in second parameter and while calling just pass one parameter. This will work fine.