Named parameters: Difference between revisions
(fortran) |
m (→{{header|Tcl}}: crosslink) |
||
Line 69: | Line 69: | ||
=={{header|Tcl}}== |
=={{header|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 (“<tt>-</tt>”) and are called options. |
The simplest way of passing named parameters is to use the Tcl language's strong support for [[Varargs#Tcl|variadic commands]] together with its arrays. By convention (originally from [[Tk]]) the named parameters names start with a hyphen (“<tt>-</tt>”) and are called options. |
||
<lang tcl>proc example args { |
<lang tcl>proc example args { |
||
# Set the defaults |
# Set the defaults |
||
Line 82: | Line 82: | ||
# => ‘foo is 0, bar is 3.14, and grill is lamb kebab’</lang> |
# => ‘foo is 0, bar is 3.14, and grill is lamb kebab’</lang> |
||
More complex option parsing is possible (e.g., with the <tt>opt</tt> package) but this is the most common way of doing it. |
More complex option parsing is possible (e.g., with the <tt>opt</tt> package) but this is the most common way of doing it. |
||
{{omit from|Java}} |
{{omit from|Java}} |
Revision as of 13:52, 29 June 2009
You are encouraged to solve this task according to the task description, using any language you may know.
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.
See also:
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) but this is the most common way of doing it.