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

Parameter passing in C/C++

I have two questions here. Q1: What will the following program output(on a 32-bit little-endian machine): int main() { long long a = 0x1, b = 0x2, c = 0x3; printf("a = %d, b = %d, c = %d.\n", a, b, c); return 0; } and why? Q2: Why the…
imsrch
  • 1,152
  • 2
  • 11
  • 24
0
votes
3 answers

Easier way to pass optional function parameters using array

When I create a function that has optional parameters, I have to think about which one is more likely to be passed the most and that determines the order in which I specify them in the function declaration. Take the below function with the not so…
imperium2335
  • 23,402
  • 38
  • 111
  • 190
0
votes
3 answers

Python get string right after argument switch

I have a script that goes using os.walk to go down a directory structure and matching files by extension and copies files to another location. This is what i have for the file…
user1957413
  • 105
  • 1
  • 12
0
votes
1 answer

php mysqli functions insisting on optional parameters

All of my mysqli functions are not accepting empty parameters. When I do so I get the following error (example): Warning: mysqli_close() expects exactly 1 parameter, 0 given... When I use mysql_close() I still get the same error. I have read the…
0
votes
3 answers

C++: Default Arguments and Vectors

In C++, I want to have a function that takes an optional argument of type vector. If the argument is not provided, I want the variable to have size 0. I currently have void v_connect::import(vector vid_,vector vpos_,vector
0
votes
2 answers

How do I declare objects as optional parameters in C#?

How do you declare a progress bar as an optional parameter? Here is the function: public int Factorial(int number, System.Windows.Forms.Label l, System.Windows.Forms.ProgressBar newprogressbar, int time=0) { .... } The function has…
robertpas
  • 643
  • 5
  • 12
  • 25
0
votes
1 answer

How to write filtered queries using SQL stored procedures?

How can I write a SQL stored procedure where I want the parameters to be optional in the select statement?
dev.net
  • 5
  • 2
0
votes
1 answer

How to parse arguments in Bash when switched AND switchless syntax is used?

I've got a bash script that can take arguments in a variety of different formats: Format 1: myscript.sh value1 value2 [value3 value4] -p -g (no switches/flags on options that have arguments, with switches for options without arguments, which strings…
Zac B
  • 3,796
  • 3
  • 35
  • 52
0
votes
1 answer

Determining difference between NULL default value and NULL argument inside a function

I'm working on a PHP class that serves as a wrapper for the Memcached class provided via a PECL extension. I am having difficulty handling a method that calls the get method. The third argument in the get method defaults to null and passes a…
tollmanz
  • 3,113
  • 4
  • 29
  • 34
0
votes
7 answers

Recommended initialization values for numbers

Assume you have a variety of number or int based variables that you want to be initialized to some default value. But using 0 could be problematic because 0 is meaningful and could have side affects. Are there any conventions around this? I have…
Gordon Potter
  • 5,802
  • 9
  • 42
  • 60
0
votes
1 answer

How to extract application return URL and other custom parameters in AD FS 2.0 claim rules?

This is probably a very basic question about AD FS 2.0 claim rule design that I haven't found an answer to (newbie). I'm probably missing something very basic, but here goes. I'm deploying a federation within a company. The company users will be…
Linus Proxy
  • 525
  • 1
  • 5
  • 21
0
votes
1 answer

LISP - cannot call function with optional parameter

I have this function in LISP with regular parameter and optional paremater n: (defun lastplus (x &optional (n 0)) //default value for n is 0 ( if (listp x) //if x is a list ( (list (length x) (n)) //return list that contains…
זאבי כהן
  • 317
  • 6
  • 16
0
votes
1 answer

Rails 3: Looping through parameters for dynamic based MySQL query

I have developed a small API that returns some minor JSON objects (mainly small resources, nothing fancy), but I am in a situation where I need to expand one of the API endpoints to return a little more than just whats there currently. For Example -…
dennismonsewicz
  • 25,132
  • 33
  • 116
  • 189
-1
votes
1 answer

Using optional parameters with a stateful widget class in flutter

Hi I am trying to create a stateful widget that accepts some required arguements and some optional ones. The optional ones will have a set default value if nothing has been passed. I have applied the same process as you would for achieving this in a…
RishV
  • 17
  • 3
-1
votes
1 answer

Is it good practice to add optional arguments to AWS lambda function?

For example, a python AWS lambda, the documented signature is: def handler(event, context): But the following is valid python syntax: def handler(event, context, *args, **kwargs): I've tested this and the lambda does not crashes. (BTW default…