Questions tagged [optional-parameters]

An optional parameter is one that a caller can include in a call to a function or method, but doesn't have to. When omitted, a default value is used instead. Optional parameters are useful when the default value is used in most cases, but still needs to be specified on occasion.

For a normal function or method, a caller must supply the number of arguments specified in the signature of the routine. However, when a function or method is declared with optional parameters, it means it's okay for a caller to omit one or more of the argument values. If the caller supplies a value for an optional parameter, the value is assigned to the parameter variable as usual. But if the caller omits a value for the corresponding argument position, the parameter is simply set to a default value instead.

Optional parameters are useful in situations where one expects that the majority of calls to the function will use the same value for that argument. Rather than forcing typical callers to include that same value in every call, the parameter can be made optional, so that callers wanting the default can simply omit the argument from the call. At the same time, in the few cases where a different value is needed, it can still be specified by just adding the extra argument.

In most languages, optional parameters must come after required parameters. When calling the function or method, the omitted parameters are at the end of the argument list. This prevents later arguments being interpreted as the value for the omitted optional parameter.

Named parameters are another way to solve this problem, as they explicitly state which parameter an argument corresponds to.

1198 questions
0
votes
1 answer

Functional dependence on optional arguments in python

I am writing some plotting routines in python. At present I can a function with some parameters and the plot function decides the best x-axis and y-axis scale to use. Is there a way for me to modify my function definition such that if I provide…
JMzance
  • 1,704
  • 4
  • 30
  • 49
0
votes
7 answers

Is it possible to have conditional parameters in C#?

EDIT: If you got here looking for the fast answer: It is possible to do this, but the overview of the function does not show the result I was looking for. The answer 'Code Contracts' by Aidin gives an error if you omit one of the optional, but…
0
votes
1 answer

Is it possible to have a variable number of parameters per function without overloading?

Is it possible to make it so that you don't have to overload a function definition 3 times if you want to be able to pass between 1 to 3 parameters into it? For example instead of: function(class a) { //this is called if only 1 class is…
Scott
  • 1,154
  • 1
  • 12
  • 25
0
votes
1 answer

RouteTable.Routes.GetVirtualPath() bug with multiple optional parameter?

In my ASP.NET MVC 4 mobile application, I have a route defined as: routes.MapRoute( name: "GraveNavigation", url: "Unites/{development}/{field1}/{field2}/{field3}/{field4}/{field5}", //each development have unique…
0
votes
3 answers

Procedure with an arbitrary number of double parameters

I have 10 double variables I would like to initialize with the value 0. They are unstructured and not part of an array by design. procedure Initialize; var a1, a2, a3, a4, a5, b1, b2, b3, b4, b5: Double; begin a1 := 0; a2 := 0; a3 := 0; …
Michel Hua
  • 1,614
  • 2
  • 23
  • 44
0
votes
1 answer

how to run a function with parameters

I have a function that simply validates forms (for old browsers). The function works just fine except that I have to pass the parameters every time I call this function, where in fact I already specified the default parameters in 'config'. So by…
KQI
  • 322
  • 1
  • 16
0
votes
1 answer

Pass Null Values from PHP to Javascript Function

I am using PHP to parse XML from the end user, and then passing that to a Javascript function to handle. I only need a few of the values to be required and the rest of the values are optional. I have tried setting the values to empty strings but am…
user470760
0
votes
1 answer

input type Reset Button Not clearing form perfectly

Here is my code: