Named parameters
Create a function which takes in a number of arguments which are specified by name rather than (necessarily) position, and show how to call the function. If the language supports reordering the arguments or optionally omitting some of them, note this.
You are encouraged to solve this task according to the task description, using any language you may know.
See also:
Ada
All callable entities (procedures, functions, entries) require named arguments. All of them can be called using either positional or keyed association of the actual arguments. The arguments supplied with default values can be omitted. <lang Ada> procedure Foo (Arg_1 : Integer; Arg_2 : Float := 0.0); </lang> It can be equivalently called as: <lang Ada> Foo (1, 0.0); Foo (1); Foo (Arg_2 => 0.0, Arg_1 => 1); Foo (Arg_1 => 1); </lang>
Fortran
Fortran accepts named parameter and optional parameter. Arguments are always named: if the name is omitted, the order used in the definition of the function / subroutine must be used.
<lang fortran> subroutine a_sub(arg1, arg2, arg3)
integer, intent(in) :: arg1, arg2 integer, intent(out), optional :: arg3 ! ... end subroutine a_sub</lang>
<lang fortran>! usage
integer :: r call a_sub(1,2, r) call a_sub(arg2=2, arg1=1)</lang>
The presence of an optional argument can be tested with PRESENT; if optional arguments come before a non optional argument, the usage of the name of the argument is mandatory.
<lang fortran> subroutine b_sub(arg1, arg2)
integer, intent(in), optional :: arg1 integer, intent(in) :: arg2 !... end subroutine b_sub</lang>
<lang fortran> call b_sub(1) ! error: missing non optional arg2
call b_sub(arg2=1) ! ok call b_sub(1, 2) ! ok: arg1 is 1, arg2 is 2 call b_sub(arg2=2, arg1=1) ! the same as the previous line</lang>
Objective C
Objective C has a method call syntax that is visually identical to named arguments, but they are not optional and may not be reordered. (The effect may be simulated by declaring multiple functions with the same leading name part and arguments reordered.) <lang objc>@interface Demo : Object {
// Omitted ...
} - (double) hypotenuseOfX: (double)x andY: (double)y; @end</lang> <lang objc>@implementation Demo
- (double) hypotenuseOfX: (double)x andY: (double)y {
return hypot(x,y);
} @end</lang> <lang objc>Demo example = [Demo new]; double h = [example hypotenuseOfX:1.23 andY:3.79];</lang> Note in the example above that the name of the method is actually “-hypotenuseOfX:andY:”.
Smalltalk
As for Objective-C the methods signature is made of "Symbol:[OtherSymbol:]*" (* stands for 0 or more repetition of the part in the brackets), without the possibility to reorder them (it would be another signature) or to make the optional.
<lang smalltalk>Object subclass: AnotherClass [
"..." initWithArray: anArray [ "single argument" ] initWithArray: anArray andString: aString [ "two args; these two methods in usage resemble a named argument, with optional andString argument" ] "..."
]</lang>
Tcl
The simplest way of passing named parameters is to use the Tcl language's strong support for variadic commands together with its arrays. By convention (originally from Tk) the named parameters names start with a hyphen (“-”) and are called options. <lang tcl>proc example args {
# Set the defaults array set opts {-foo 0 -bar 1 -grill "hamburger"} # Merge in the values from the caller array set opts $args # Use the arguments return "foo is $opts(-foo), bar is $opts(-bar), and grill is $opts(-grill)"
}
- Note that -foo is omitted and -grill precedes -bar
example -grill "lamb kebab" -bar 3.14
- => ‘foo is 0, bar is 3.14, and grill is lamb kebab’</lang>
More complex option parsing is possible, e.g., with the opt package (of which only a small fraction of the functionality is shown here). This package also allows you to specify type constraints, though that is usually not necessary: <lang tcl>package require opt tcl::OptProc example {
{-foo -int 0 "The number of foos"} {-bar -float 1.0 "How much bar-ness"} {-grill -any "hamburger" "What to cook on the grill"}
} {
return "foo is $foo, bar is $bar, and grill is $grill"
} example -grill "lamb kebab" -bar 3.14
- => ‘foo is 0, bar is 3.14, and grill is lamb kebab’
example -help
- Usage information:
- Var/FlagName Type Value Help
- ------------ ---- ----- ----
- ( -help gives this help )
- -foo int (0) The number of foos
- -bar float (1.0) How much bar-ness
- -grill any (hamburger) What to cook on the grill</lang>