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
-1
votes
2 answers

Check if optional byte argument was passed in

I want to let a user specify a custom color for a class in the constructor via passing in RGBA bytes. If they do not specify a custom color, a default color from the application settings will be used. If the Alpha is left out, however, assume fully…
Michael
  • 425
  • 2
  • 9
-1
votes
1 answer

"Optional parameters should not be used" error thrown in SonarQube analysis

I'm getting error - "Optional parameters should not be used" in Sonar analysis for C# project. Actually in C# latest version we can define optional parameter but SonarQube analysis says Optional parameters should not be used. How can I solve this…
Rasik Bapotra
  • 35
  • 1
  • 8
-1
votes
1 answer

Pass custom variables in MySQL connection

I am setting up a MySQL connection (in my case PDO but it shouldn't matter) in a REST API. The REST API uses an internal authentication (username / password). There are multiple user groups accessing the REST API, e.g. customers, IT, backend,…
Horen
  • 11,184
  • 11
  • 71
  • 113
-1
votes
1 answer

What does colon before a constant in Ruby mean?

Here is the example: def container(number=:FIXME) "bottles" end Excerpt From: Sandi Metz, Katrina Owen. “99 Bottles of OOP.” iBooks.
Ekaitz Hernandez Troyas
  • 1,147
  • 2
  • 11
  • 18
-1
votes
2 answers

UIImage as Optional

Hi I want users to be able to Register even if they don't choose a profile picture Right now I use this code for the profile picture: let profileImageData = UIImageJPEGRepresentation((userImage.image!), 1) if (profileImageData != nil) { …
Kerby Jean
  • 207
  • 2
  • 12
-1
votes
1 answer

Does optional parameter and optional attribute not supported together?

public void ObjTest(StringBuilder sb, List list, int i = 0, [Optional] string bs) { ...... } The above code throwing compilation error " Optional parameters must appear after all required parameters ". Does optional parameter and…
Anoob K A
  • 59
  • 8
-1
votes
1 answer

How can I deal with this error in the autoregressive model parameter estimation?

I am trying to use the following code for my autoregressive model parametere estimation: ar(file[,1], aic = TRUE, order.max = NULL,method = "mle") Then, I have the results along with the following errors: Call: ar(x = file[, 1], aic = TRUE,…
user3319993
-1
votes
1 answer

Baked Optional Parameters and WebService calls in C#?

AFAIK: Adding an optional parameter to a public method that’s called from another assembly requires recompilation of both assemblies— just as though the parameter were mandatory. I was wondering about WebService in this context. What about me…
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
-1
votes
1 answer

Default nvarchar parameter not respected

I have a stored procedure in SQL Server 2008 R2 with the following parameter values declared: @UN nvarchar(30), @SN nvarchar(8), @GG uniqueidentifier, @Ss irnapp.SymbolTableType READONLY, @SD date, @ED date, @IR nvarchar(1), @ID nvarchar(1), @NR int…
-2
votes
4 answers

How can I pass function that has optional parameter from parent widget to child widget?

I cannot use function that has optional parameter in child widget(name is FixUI).This is original function declared in parent widgetThis is the line error occured(red line: Underfined~) I want to make fixContact function arguments : fixContact,…
MIN S
  • 21
  • 1
-2
votes
1 answer

Javascript addeventlistener working without parameters

I was looking for javascript documentation and found this const log = document.getElementById('log'); document.addEventListener('keydown', logKey); function logKey(e) { log.textContent += ` ${e.code}`; } I don't understand how logkey function…
-2
votes
2 answers

How to add sql 'when' functionality in c# linq

I have the following parameters: public object GetDataByProjectCostID(string employeeid, DateTime costdate, int id = 0) And the query: var projectCost = (from pc in db.ProjectCosts where pc.ProjectCostID == id …
Nur Uddin
  • 1,798
  • 1
  • 28
  • 38
-2
votes
1 answer

How can I force the use of a default parameter value in C# (skip optional arguments)?

If I am using a method with multiple optional parameters, how can I call the method without specifying only one of the parameters, letting it use its default value? Say I have method Foo with multiple optional parameters: void Foo(string a, int b =…
-2
votes
2 answers

How can I retrieve single values from passed objects in C#?

Prior to this approach, I had some methods that included four or five parameters, so I want to condense the majority of them into an object. The TestOptions() object has additional optional values, like 'name' or 'location'. I'm having trouble…
Dan
  • 29
  • 6
-2
votes
1 answer

jquery shorthand return variable undefined

EDIT: Seems I describe my problem to complicated: My Question below is: WHY does my shorthand code only work when my input-variable contains '=' and NOT when it doesn't ? I retrieve the variable with this code: var hash =…
XanderMan
  • 137
  • 1
  • 15
1 2 3
79
80