Optional parameters: Difference between revisions

(added c++)
Line 521:
 
See the Python entry in [[Named_Arguments#Python|Named Arguments]] for a more comprehensive description of Python function parameters and call arguments.
 
Note that expression for a default argument of an optional parameter is evaluated only once, and all calls of the function where that parameter is missing will be initialized to point to that same shared object. So, if the default argument value is a mutable object (e.g. list, dict, etc.), then any changes to it will affect what is seen by future calls of the function:
<pre>
>>> def foo(x, lst=[]):
... lst.append(x)
... print lst
...
>>> foo(1)
[1]
>>> foo(2)
[1, 2]
>>> foo(3)
[1, 2, 3]
</pre>
 
=={{header|Ruby}}==
Anonymous user