+ 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.

31st May 2018, 7:43 AM
Kasul Kliff
Kasul Kliff - avatar
4 ответов
+ 6
Here in js. var i = 4; function h(a,d = a+2) { alert(d); } h(i);
31st May 2018, 8:09 AM
Warith Vatanaplachaigoon
Warith Vatanaplachaigoon - avatar
+ 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.
31st May 2018, 7:49 AM
Sachin Artani
Sachin Artani - avatar
+ 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
31st May 2018, 8:12 AM
Max
Max - avatar
+ 1
why don't you put default value in second parameter and while calling just pass one parameter. This will work fine.
31st May 2018, 7:50 AM
Pulkit Kamboj
Pulkit Kamboj - avatar