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
149
votes
9 answers

C# 4.0: Can I use a TimeSpan as an optional parameter with a default value?

Both of these generate an error saying they must be a compile-time constant: void Foo(TimeSpan span = TimeSpan.FromSeconds(2.0)) void Foo(TimeSpan span = new TimeSpan(2000)) First of all, can someone explain why these values can't be determined at…
Mike Pateras
  • 14,715
  • 30
  • 97
  • 137
143
votes
6 answers

How to create default value for function argument in Clojure

I come with this: (defn string->integer [str & [base]] (Integer/parseInt str (if (nil? base) 10 base))) (string->integer "10") (string->integer "FF" 16) But it must be a better way to do this.
jcubic
  • 61,973
  • 54
  • 229
  • 402
127
votes
11 answers

method overloading vs optional parameter in C# 4.0

which one is better? at a glance optional parameter seems better (less code, less XML documentation, etc), but why do most MSDN library classes use overloading instead of optional parameters? Is there any special thing you have to take note when you…
Louis Rhys
  • 34,517
  • 56
  • 153
  • 221
124
votes
8 answers

Ruby optional parameters

If I define a Ruby functions like this: def ldap_get ( base_dn, filter, scope=LDAP::LDAP_SCOPE_SUBTREE, attrs=nil ) How can I call it supplying only the first 2 and the last args? Why isn't something like ldap_get( base_dn, filter, , X) possible…
Bruno Antunes
  • 2,241
  • 4
  • 21
  • 35
119
votes
10 answers

How to check whether optional function parameter is set

Is there an easy way in Python to check whether the value of an optional parameter comes from its default value, or because the user has set it explicitly at the function call?
Matthias
  • 9,817
  • 14
  • 66
  • 125
115
votes
21 answers

How would I skip optional arguments in a function call?

OK I totally forgot how to skip arguments in PHP. Lets say I have: function getData($name, $limit = '50', $page = '1') { ... } How would I call this function so that the middle parameter takes the default value (ie. '50')? getData('some name',…
Sebastian
110
votes
6 answers

Set default value for DateTime in optional parameter

How can I set default value for DateTime in optional parameter? public SomeClassInit(Guid docId, DateTime addedOn = DateTime.Now???) { //Init codes here }
Sadegh
  • 4,181
  • 9
  • 45
  • 78
108
votes
5 answers

How does one make an optional closure in swift?

I'm trying to declare an argument in Swift that takes an optional closure. The function I have declared looks like this: class Promise { func then(onFulfilled: ()->(), onReject: ()->()?){ if let callableRjector = onReject { // do…
Marcosc
  • 3,101
  • 4
  • 17
  • 16
107
votes
3 answers

Passing an empty array as default value of an optional parameter

How does one define a function that takes an optional array with an empty array as default? public void DoSomething(int index, ushort[] array = new ushort[] {}, bool thirdParam = true) results in: Default parameter value for 'array' must be a…
MandoMando
  • 5,215
  • 4
  • 28
  • 35
98
votes
7 answers

Cannot use String.Empty as a default value for an optional parameter

I am reading Effective C# by Bill Wagner. In Item 14 - Minimize Duplicate Initialization Logic, he shows the following example of using the new optional parameters feature in a constructor: public MyClass(int initialCount = 0, string name =…
Mikeyg36
  • 2,718
  • 4
  • 24
  • 25
98
votes
13 answers

Should you declare methods using overloads or optional parameters in C# 4.0?

I was watching Anders' talk about C# 4.0 and sneak preview of C# 5.0, and it got me thinking about when optional parameters are available in C# what is going to be the recommended way to declare methods that do not need all parameters specified? For…
Greg Beech
  • 133,383
  • 43
  • 204
  • 250
80
votes
14 answers

A javascript design pattern for options with default values?

// opt_options is optional function foo(a, b, opt_options) { // opt_c, opt_d, and opt_e are read from 'opt_options', only c and d have defaults var opt_c = 'default_for_c'; var opt_d = 'default_for_d'; var opt_e; // e has no default if…
ripper234
  • 222,824
  • 274
  • 634
  • 905
80
votes
9 answers

C# optional parameters on overridden methods

Seems like in .NET Framework there is an issue with optional parameters when you override the method. The output of the code below is: "bbb" "aaa" . But the output I'm expecting is: "bbb" "bbb" .Is there a solution for this. I know it can be solved…
SARI
  • 854
  • 1
  • 8
  • 12
76
votes
3 answers

The compiler is complaining about my default parameters?

I'm having trouble with this piece of code , after i took this class from the main.cpp file and splitted it in to .h and .cpp the compiler started complaining about the default parameters i was using in a void. /* PBASE.H */ class pBase : public…
Christian
  • 1,492
  • 3
  • 16
  • 19
76
votes
6 answers

Optional parameters for interfaces

Using c# 4.0 -- building an interface and a class that implements the interface. I want to declare an optional parameter in the interface and have it be reflected in the class. So, I have the following: public interface IFoo { void Bar(int…
bryanjonker
  • 3,386
  • 3
  • 24
  • 37
1
2
3
79 80