Questions tagged [optional-arguments]

An optional argument is an argument which can be omitted, eventually being replaced by a default value, where an argument is an actual value passed to a function, procedure, or command line program.

223 questions
393
votes
23 answers

Named tuple and default values for optional keyword arguments

I'm trying to convert a longish hollow "data" class into a named tuple. My class currently looks like this: class Node(object): def __init__(self, val, left=None, right=None): self.val = val self.left = left self.right =…
sasuke
  • 6,589
  • 5
  • 36
  • 35
247
votes
11 answers

C# 4.0 optional out/ref arguments

Does C# 4.0 allow optional out or ref arguments?
Joe Daley
  • 45,356
  • 15
  • 65
  • 64
231
votes
2 answers

Why is the empty dictionary a dangerous default value in Python?

I put a dict as the default value for an optional argument to a Python function, and pylint (using Sublime package) told me it was dangerous. Can someone explain why this is the case? And is a better alternative to use None instead?
tscizzle
  • 11,191
  • 15
  • 54
  • 88
186
votes
7 answers

LaTeX Optional Arguments

How do you create a command with optional arguments in LaTeX? Something like: \newcommand{\sec}[2][]{ \section*{#1 \ifsecondargument and #2 \fi} } } Then, I can call it like \sec{Hello} %Output:…
Verhogen
  • 27,221
  • 34
  • 90
  • 109
160
votes
9 answers

How to pass optional arguments to a method in C++?

How to pass optional arguments to a method in C++ ? Any code snippet...
Swapnil Gupta
  • 8,751
  • 15
  • 57
  • 75
87
votes
9 answers

How do I test if optional arguments are supplied or not?

How do I test if optional arguments are supplied or not? -- in VB6 / VBA Function func (Optional ByRef arg As Variant = Nothing) If arg Is Nothing Then <----- run-time error 424 "object required" MsgBox "NOT SENT" End If End…
Robin Rodricks
  • 110,798
  • 141
  • 398
  • 607
54
votes
4 answers

getopt does not parse optional arguments to parameters

In C, getopt_long does not parse the optional arguments to command line parameters parameters. When I run the program, the optional argument is not recognized like the example run below. $ ./respond --praise John Kudos to John $ ./respond --blame…
hayalci
  • 4,089
  • 2
  • 27
  • 30
40
votes
6 answers

Is there a way to use two '...' statements in a function in R?

I want to write a function that calls both plot() and legend() and it would be ideal if the user could specify a number of additional arguments that are then passed through to either plot() or legend(). I know I can achieve this for one of the two…
Henrik
  • 14,202
  • 10
  • 68
  • 91
30
votes
2 answers

Multiple optional parameters calling function

Assume that i have a function like this below It takes 3 parameters and 2 have optional values private void myfunc (int a, int b=2, int c=3) { //do some stuff here related to a,b,c } now i want to call this function like below how possible…
30
votes
3 answers

Python: argparse optional arguments without dashes

I would like to have the following syntax: python utility.py file1 FILE1 file2 FILE2 where file1 and file2 are optional arguments. It is simple to make it working with this syntax: python utility.py --file1 FILE1 --file2…
jvm
  • 349
  • 1
  • 3
  • 7
24
votes
3 answers

Python optional parameters

Guys, I just started python recently and get confused with the optional parameters, say I have the program like this: class B: pass class A: def __init__(self, builds = B()): self.builds = builds If I create A twice b = A() c =…
user192048
  • 475
  • 2
  • 4
  • 10
19
votes
2 answers

Python: unused argument needed for compatibility. How to avoid Pylint complaining about it

For my code in Python, I would like to call many functions with a specific argument. However, for some functions, that argument does not do anything. Still, I would like to add the argument for compatibility reasons. For example, consider the…
EdG
  • 328
  • 1
  • 2
  • 8
19
votes
3 answers

Why does C# allow ambiguous function calls through optional arguments?

I came across this today, and I am surprised that I haven't noticed it before. Given a simple C# program similar to the following: public class Program { public static void Main(string[] args) { Method(); // Called the method with no…
Adam Goodwin
  • 3,951
  • 5
  • 28
  • 33
19
votes
7 answers

Fortran 2003 / 2008: Elegant default arguments?

In fortran, we can define default arguments. However, if an optional argument is not present, it can also not be set. When using arguments as keyword arguments with default values, this leads to awkward constructs like PROGRAM PDEFAULT CALL…
17
votes
1 answer

Can you explain me this strange behaviour of c# with optional arguments?

Possible Duplicate: C# optional parameters on overridden methods This is the output of the following code: Peter: -1 Peter: 0 Fred: 1 Fred: 1 Can you explain me why the call of Peter p.TellYourAge() and p.DoSomething() is not identical? Here the…
X181
  • 753
  • 1
  • 5
  • 12
1
2 3
14 15