Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why optional parameters must appear at the end of the declaration

Tags:

In all programming languages supporting optional parameters that I have seen there is a imitation that the optional parameters must appear at the end of the declaration. No required parameters may be included after an optional item. What is the reason for that ? I guess it can be compiler/interpreter requirement.

like image 753
Incognito Avatar asked May 24 '10 10:05

Incognito


People also ask

Why are the optional parameters are added?

Optional Parameters are parameters that can be specified, but are not required. This allows for functions that are more customizable, without requiring parameters that many users will not need.

Why should the required arguments appear before the optional arguments in a function definition?

Optional parameters must appear after required arguments when you define a function. Required arguments are those that do not have a default value assigned. This is because if optional arguments came before required arguments then it will eliminate the purpose of optional arguments.

What is the use of optional parameter?

What are Optional Parameters? By definition, an Optional Parameter is a handy feature that enables programmers to pass less number of parameters to a function and assign a default value.

Is it mandatory to specify a default value to optional parameter?

Every optional parameter in the procedure definition must specify a default value. The default value for an optional parameter must be a constant expression. Every parameter following an optional parameter in the procedure definition must also be optional.


1 Answers

Well, if they were at the front, how would you detect when they've stopped being supplied? The only way would be if the variable type was different after the optional parameters. Bit of a weird requirement, so it makes sense that you just force them to be last (save the trouble of complex rules for detecting the "final" optional parameter).

Besides, it's the most natural way to do it when calling the function.

like image 124
Noon Silk Avatar answered Sep 20 '22 09:09

Noon Silk