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

How to write the following method elegantly?

I have the following method signature: Party cloneParty(bool useGivenParams, int i_PartyId, PartyRole i_PartyRole, DeletedState i_Deleted) and also a member collection of existing parties called m_PartiesById. Is there an elegant way to write the…
felisimo
  • 198
  • 14
-2
votes
1 answer

How can I convert optional parameter method declared in C into c++

I'm converting some old C-code into c++ and come across something strange, the C-code declared in .h file says: void Message(char *s, ...); and implementation head in .c void Message(char *s, long x1, long x2, long x3, long x4, long x5, long…
Anders S
  • 187
  • 11
-2
votes
1 answer

Don't get compilation error when using three dots notation for parameters

I want to generate CSV files. I have a generateCSV method which takes a filename as parameters and a bunch of entries private static void generateCSV(String pFilename, String... pColumns) { try(FileWriter fileWriter = new…
-2
votes
1 answer

Default construct an optional function parameter

I want to design a function so that the parameter is optional and default constructed if left out. The parameter is a struct that I've defined in the same header file. The tried to use the following function declaration: void publish(MarkerStyle…
user2445507
  • 443
  • 3
  • 13
-2
votes
1 answer

Google map api3, Routing with address search and waypoints

i have a question about using the waypoint feature ,i took the script from the User Geocodezip; very big thanks for that ,and it work well ,but i can only route with waypoint ,i would like to have the waypoints as an option,but if the User let this…
-2
votes
2 answers

C# What happens when you pass null to an optional argument

If I have a function with an optional argument like so: public string testFunction (string arg1 = "Adam"){ return "Hello " + arg1; } If I pass a null string to the function, will the function return: "Hello Adam" Or: "Hello " (Hello null)
Adam Nygate
  • 325
  • 3
  • 14
-3
votes
2 answers

MSVC: "too few arguments in function call" when omitting optional parameter

in a WDF driver project I'm writing in Visual Studio 2013, I've got this function: ZwWaitForSingleObject(hSemaphore, 0); which gives the "too few arguments in function call" error, if I omit the last parameter, which is optional: NTSTATUS…
Flavio
  • 451
  • 3
  • 26
-4
votes
3 answers

Check if Optional Parameter has changed

i have a Function with1 Parameter and 10 optional Parameters. The optional Parameters are all nothing. Now i want to check if a optional Parameter had changed his Value or the Function was started with an optional Parameter. Think of it that the…
JJonBacon
  • 3
  • 1
-4
votes
1 answer

C++ STL map and set containers' insert() function violating the rules for default parameters?

insert() for a set takes the parameters: (set::iterator, int) The iterator parameter is optional to enter, and so I assume it must have a default value in its implementation. Now, aren't parameters with default value supposed to be at the end?…
Aayush Mahajan
  • 3,856
  • 6
  • 25
  • 32
-4
votes
2 answers

Python global variables

I'm new to learning python, and this has been troubling me for a while, any help would be much appreciated! I would like it to create a list of increasing numbers. (I know an easier way of doing this, but this is a simplified example of the problem…
Thomas P G
  • 37
  • 3
-5
votes
2 answers

How do I use the '?' operator in mvc?

I have an MVC application and I want to use this ? option. Please tell me how I can do this. I have written: int? isfeature; but, while inserting the value in database, how do I use it? It is giving following error: Cannot convert from 'int?' to…
Ritz
  • 97
  • 1
  • 8
-8
votes
3 answers

Java: Parameter list without comma

I came across a method with a parameter list where the parameter were not separated by comma and no declaration of the variable type: public int compareWith(Choice anotherChoice){ Later these parameters were called without any further declaration…
GDanCab
  • 3
  • 1
1 2 3
79
80