how can we pass default values to formal parameters while defining a function ?
like in c++ we can do…
void fun1(int a=3){
…
…
}
how can we pass default values to formal parameters while defining a function ?
like in c++ we can do…
void fun1(int a=3){
…
…
}
we cant pass default var like c++ , but you can use the Builder Pattern, as described in this Stack Overflow answer.
As described in the linked answer, the Builder Pattern lets you write code like
Student s1 = new StudentBuilder().name("Eli").buildStudent();
Student s2 = new StudentBuilder()
.name("Spicoli")
.age(16)
.motto("Aloha, Mr Hand")
.buildStudent();
in which some fields can have default values or otherwise be optional