Questions tagged [default-parameters]

A default parameter is a function or method parameter that has a default value provided to it. If the user does not supply a value for this parameter, the default value will be used. If the user does supply a value for the default parameter, the user supplied value is used.

A default parameter is a function or method parameter that has a default value provided to it. If the user does not supply a value for this parameter, the default value will be used. If the user does supply a value for the default parameter, the user supplied value is used.

366 questions
3352
votes
34 answers

"Least Astonishment" and the Mutable Default Argument

Anyone tinkering with Python long enough has been bitten (or torn to pieces) by the following issue: def foo(a=[]): a.append(5) return a Python novices would expect this function called with no parameter to always return a list with only…
Stefano Borini
  • 138,652
  • 96
  • 297
  • 431
2638
votes
29 answers

Set a default parameter value for a JavaScript function

I would like a JavaScript function to have optional arguments which I set a default on, which get used if the value isn't defined (and ignored if the value is passed). In Ruby you can do it like this: def read_file(file, delete_after = false) #…
Tilendor
  • 48,165
  • 17
  • 52
  • 58
2098
votes
28 answers

Does Java support default parameter values?

I came across some Java code that had the following structure: public MyParameterizedFunction(String param1, int param2) { this(param1, param2, false); } public MyParameterizedFunction(String param1, int param2, boolean param3) { //use all…
gnavi
  • 25,122
  • 7
  • 21
  • 11
360
votes
23 answers

C default arguments

Is there a way to specify default arguments to a function in C?
Nathaniel Flath
  • 15,477
  • 19
  • 69
  • 94
220
votes
10 answers

How can I avoid issues caused by Python's early-bound default parameters (e.g. mutable default arguments "remembering" old data)?

Sometimes it seems natural to have a default parameter which is an empty list. However, Python produces unexpected behavior in these situations. For example, consider this function: def my_func(working_list=[]): working_list.append("a") …
John Mulder
  • 9,765
  • 7
  • 33
  • 37
183
votes
4 answers

T-SQL - function with default parameters

I have this script: CREATE FUNCTION dbo.CheckIfSFExists(@param1 INT, @param2 BIT = 1 ) RETURNS BIT AS BEGIN IF EXISTS ( bla bla bla ) RETURN 1; RETURN 0; END GO I want to use it in a procedure in this way: IF dbo.CheckIfSFExists(…
nirmus
  • 4,913
  • 9
  • 31
  • 50
104
votes
6 answers

C# 4 default parameter values: How to assign a default DateTime/object value?

If DateTime is an object and default C# parameters can only be assigned compile-time constants, how do you provide default values for objects like DateTime? I am trying to initialize values in a POCO with a constructor, using named parameters with…
Zachary Scott
  • 20,968
  • 35
  • 123
  • 205
87
votes
2 answers

Python, default keyword arguments after variable length positional arguments

I thought I could use named parameters after variable-length positional parameters in a function call in Python 2, but I get a SyntaxError when importing a python class. I'm writing with the following "get" method, for example: class Foo(object): …
63
votes
4 answers

Default template argument and partial specialization

Please explain to me why the following piece of code complies and works perfectly. I am very confused. #include template class Base {}; template class Base { public: Base() { …
Andy
  • 701
  • 1
  • 7
  • 7
61
votes
4 answers

Using an attribute of the current class instance as a default value for method's parameter

Possible Duplicate: default value of parameter as result of instance method While it is possible to set default values to function parameters in python: def my_function(param_one='default') ... It seems not to be possible to access the…
paweloque
  • 18,466
  • 26
  • 80
  • 136
60
votes
3 answers

Is there a way to use the default value on a non-optional parameter when null is passed?

For example, if I have the following data class: data class Data( val name: String = "", val number: Long = 0 ) And functions that can return null: fun newName(): String? {} fun newNumber(): Long? {} I know I can use the following to use…
Bryan
  • 14,756
  • 10
  • 70
  • 125
50
votes
3 answers

Type Hinting: Default Parameters

PHP 5 Type Hinting PHP 5 introduces Type Hinting. Functions are now able to force parameters to be objects (by specifying the name of the class in the function prototype) or arrays (since PHP 5.1). However, if NULL is used as the default parameter…
ThinkingMonkey
  • 12,539
  • 13
  • 57
  • 81
49
votes
3 answers

Cost of Default parameters in C++

I stumbled through an example from "Effective C++ in an Embedded Environment" by Scott Meyers where two ways of using default parameters were described: one which was described as costly and the other as a better option. I am missing the explanation…
bentz123
  • 1,081
  • 7
  • 16
43
votes
9 answers

Why are default arguments evaluated at definition time?

I had a very difficult time with understanding the root cause of a problem in an algorithm. Then, by simplifying the functions step by step I found out that evaluation of default arguments in Python doesn't behave as I expected. The code is as…
Mert Nuhoglu
  • 9,695
  • 16
  • 79
  • 117
35
votes
2 answers

Function pointers with default parameters in C++

How does C++ handle function pointers in relation to functions with defaulted parameters? If I have: void foo(int i, float f = 0.0f); void bar(int i, float f); void (*func_ptr1)(int); void (*func_ptr2)(int, float); void (*func_ptr3)(int, float =…
1
2 3
24 25