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

Using optional parameter in widget

I am getting a variable to a widget optionally and i want to use it. class MainPage extends StatefulWidget { final String? uyeId; MainPage({ Key? key, this.uyeId, }) : super(key: key); @override State createState() =>…
-1
votes
1 answer

junit test case for optional parameters

@GetMapping("/getAllProjectsDetails") public List getAllProjectsDetails(@RequestParam Optional projectId, @RequestParam Optional createdBy, @RequestParam Optional projectDescription, …
-1
votes
3 answers

Declaring an optional argument of type int and testing its existence

In ActionScript 3 you can declare optional parameters like this: function (i:int = 0, s:String = "", o:Object = null):void { } So you can check if the user passed parameters s and o because you can test against the empty string or the null object…
Tom Auger
  • 19,421
  • 22
  • 81
  • 104
-1
votes
3 answers

Is there a way to explicitly call optional arguments in c++?

When I create a function such as: int addThree(int x=1, int y=1, int z=1) I want to call the function such that it uses the default arguments for x and z, but not y. Some attempts have been addThree(5,,5) and addThree(5,NULL,5), but neither work…
-1
votes
2 answers

How to to pass optional parameters from one function to another function, which is also a parameter of the first function?

The title seems complicated but the problem itself is easy to describe with an example: func has another function as a parameter (either my_func1 or my_func2) and I need to provide the parameters to these two function but my_func1 needs three…
JALC
  • 1
-1
votes
2 answers

Optional Params In Java

is there a way if repeat is "false", then i dont want to use repeat and repeatMode anymore ? Instead of Overload, Can method ignore those params ? void task(String title,boolean repeat, String repeatMode){ }
Meghana
  • 111
  • 7
-1
votes
2 answers

How to make API with required and optional parameters | PHP

Suppose there is a custom API to get giftcode. The API has Referance and Amount as requird parameters whereas Activated and Quantity as optional parameters If URL is: http://ruteurl/rest/V1/giftcode/request?reference=XYZ&amount=50 It should run…
Ajwad Syed
  • 335
  • 2
  • 15
-1
votes
1 answer

multiple parameters not obligatory

I want to write a code with multiple parameters like this : Set param1 = qt.Parameters.Add("City Parameter", xlParamTypeVarChar) and be able to use either one parameter or the second. VBA obliged me to have values for both parameters, but I want to…
-1
votes
2 answers

Inheritance chain with optional parameter

I wanted an inheritance chain to have an optional parameter. Most classes along the chain need to have the parameter become a member sometimes, but I also want to use the chain other times without the parameter becoming a member. I thought of…
bad_coder
  • 11,289
  • 20
  • 44
  • 72
-1
votes
1 answer

Optional params in Go?

I know that there aren't any optional params in the latest version of Go. But there are quite a lot cases when they are really helpful. Consider oversimplified example: func getFullName(firstName string, lastName string, maybeMiddleName func()…
Zazaeil
  • 3,900
  • 2
  • 14
  • 31
-1
votes
1 answer

getOrElse for an Option(null) does not return a None type or the default value when null in Scala

I am trying to initialize a list in Spark using scala, from a column of data. The value in some of the rows can be empty, and in some rows it's filled. So I am making my list of type Option[String]. But, when I access the elements in it, scala…
sparkonhdfs
  • 1,313
  • 2
  • 17
  • 31
-1
votes
1 answer

Both GET and POST on webapi with POST with a parameter but GET with no param

I am trying a webapi which will support both GET and POST but the POST method needs to pass an alphanumeric parameter BUT GET should ignore any parameters. [HttpPost] [HttpGet] [Route("payment")] public virtual async…
sajanyamaha
  • 3,119
  • 2
  • 26
  • 44
-1
votes
2 answers

Chance of breaking existing code by adding optional parameter to VB.NET function?

Is there a chance that existing code in a large project might bomb if I add a new optional parameter to a function that's used everywhere? I know I can overload the function instead and minimize the risk, but really.. what's the risk if I insist on…
ShieldOfSalvation
  • 1,297
  • 16
  • 32
-1
votes
1 answer

Optional closure with Bool return type

I have a method with an optional closure argument like this: func when(_ name:String, state:State = .normal, closure:(() -> Bool)? = nil) { ... } I call them like this: when("I do something") { if !self.doSomething() { return false } if…
BadmintonCat
  • 9,416
  • 14
  • 78
  • 129
-1
votes
2 answers

is there a way to make a list of optional params? python

Let's say I have a function with one / two option but what if I want to add more later on and more more? Is there a way to make it work without much hassle? def json_response_message(status, message, option=(), option1=()): data = { …
Tsuna
  • 2,098
  • 6
  • 24
  • 46