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
17
votes
3 answers

Optional argument cannot be erased?

I wanted to have a tail-recursive version of List.map, so I wrote my own. Here it is: let rec list_map f l ?(accum=[])= match l with head :: tail -> list_map f tail ~accum:(head :: accum) | [] -> accum;; Whenever I compile this…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
15
votes
2 answers

Multiple optional arguments python

So I have a function with several optional arguments like so: def func1(arg1, arg2, optarg1=None, optarg2=None, optarg3=None): Optarg1 & optarg2 are usually used together and if these 2 args are specified then optarg3 is not used. By contrast, if…
e9e9s
  • 885
  • 2
  • 13
  • 24
14
votes
3 answers

Generic methods and optional arguments

Is it possible to write similar construction? I want to set, somehow, default value for argument of type T. private T GetNumericVal(string sColName, T defVal = 0) { string sVal = GetStrVal(sColName); T nRes; if…
hardsky
  • 514
  • 5
  • 15
13
votes
3 answers

Propagating optional arguments

The following code does not compile. type A(?arg) = member __.Arg : string option = arg type B(?arg) = inherit A(arg) //ERROR expected type string but has type 'a option I assume this is because an instance of the underlying type of the option…
Daniel
  • 47,404
  • 11
  • 101
  • 179
13
votes
4 answers

How to write a test for a function with optional arguments

I want to test function calls with optional arguments. Here is my code: list_get() list_get(key, "city", 0) list_get(key, 'contact_no', 2, {}, policy) list_get(key, "contact_no", 0) list_get(key, "contact_no", 1, {}, policy, "") list_get(key,…
Pavan Gupta
  • 17,663
  • 4
  • 22
  • 29
12
votes
1 answer

Map Update method with ifAbsent in Dart

I'd like to modify an existing item in a map, as in replace the value of an existing key with a new one, with an added clause if the key does not exist in the Map already, to simply create a new key and value pair. The Dart documentation suggests…
11
votes
5 answers

Function with dictionaries as optional arguments - Python

I'm trying to create a function that might receive as input many or a few dictionaries. I'm using the following code: def merge_many_dics(dic1,dic2,dic3=True,dic4=True,dic5=True,dic6=True,dic7=True,dic8=True,dic9=True,dic10=True): """ Merging up to…
aabujamra
  • 4,494
  • 13
  • 51
  • 101
10
votes
2 answers

Why default arguments in F# (FSharpOption) are reference types?

C# and F# has different implementation of the default (or optional) parameters. In C# language when you add default value to the argument you'll not change its underlying type (I mean type of the parameter). Actually optional arguments in C# is a…
Sergey Teplyakov
  • 11,477
  • 34
  • 49
9
votes
2 answers

Python 3.5 TypeError: got multiple values for argument

def f(a, b, *args): return (a, b, args) f(a=3, b=5) (3, 5, ()) whereas: f(a=3, b=5, *[1,2,3]) TypeError: got multiple values for argument 'b' Why it behaves like this? Any particular reason?
Jan Sikora
  • 91
  • 1
  • 1
  • 4
9
votes
1 answer

Is it necessary to check an optional argument before passing it to another optional argument?

I have the following question concerning the usage of optional argument. Let's say I have the following routine aaa defined in module m_aaa MODULE m_aaa SUBROUTINE aaa(a, b) INTEGER :: a INTEGER, OPTIONAL :: b END SUBROUTINE END…
9
votes
3 answers

Optional arguments with default value in Ruby

I would like to create a function that has optional arguments with default values def my_function(a = nil, b=nil, c=500) end and call the function with the arguments I would like to specify only my_function(b=100) How do I accomplish this in Ruby…
AdamNYC
  • 19,887
  • 29
  • 98
  • 154
8
votes
6 answers

Assign pass to a function in Python

I have a piece of code that defines a function that takes a function as an argument, like so: def stuff(n, f): f(n) Now, I want to provide some default value of f that does nothing. So I figured I'd use pass, like so: def stuff(n, f = None): …
8
votes
2 answers

Matlab - Optional handle argument first for plot like functions

Matlab includes many plotting functions which take an optional argument being the handle to the axis to plot to. There are many solutions online for adding optional arguments to user-defined functions (varargin, inputParser), however they usually…
bhillam
  • 193
  • 6
8
votes
3 answers

Can I default a function argument to the value of __FILE__ at the caller?

In C++, can I have a defaulted argument to a function which defaults to __PRETTY_FUNCTION___, ___FILE___, and ___LINE__ as defined at the point of the caller and not the point the defaults are supplied in a header file without using macros?
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
7
votes
2 answers

How to retrieve the value(s) of a parameter declared with vararg in an enum in Kotlin

I am new to Kotlin and I have an enum containing many values, those values refer to different states my application has. Now I need to log something whenever the app enters a state but some state in the enum can log more than one thing (based on…
Cliff Burton
  • 3,414
  • 20
  • 33
  • 47
1
2
3
14 15