Jump to content

Optional parameters: Difference between revisions

(→‎{{header|F#}}: added F#)
Line 556:
{{trans|Oz}}
 
F# supports optional parameters for member functionsmembers only, not for free-standing functions.
 
Optional parameters are marked by using a question mark in front of the identifier. Their values are passed as option types, i.e. as <code>Some value</code> or <code>None</code>. The helper function <code>defaultArg</code> can be used to specify default values. In the example below, we use shadowing in order to reuse the identifiers <code>ordering</code>, <code>column</code> and <code>reverse</code>.
 
Typically, parameters are named at the caller site when optional parameters are involved. However, this is not technically required as long as only right-most arguments are omitted.
 
<lang fsharp>type Table(rows:string[][]) =
Line 597 ⟶ 599:
t.Sort(ordering=fun s1 s2 -> compare s2.Length s1.Length)
printfn "Sorted by decreasing length"; t.Print()</lang>
 
Output:
<pre>Unsorted
[|"a"; "b"; "c"|]
[|""; "q"; "z"|]
[|"can"; "z"; "a"|]
Default sort
[|""; "q"; "z"|]
[|"a"; "b"; "c"|]
[|"can"; "z"; "a"|]
Sorted by col. 2
[|"can"; "z"; "a"|]
[|"a"; "b"; "c"|]
[|""; "q"; "z"|]
Sorted by col. 1
[|"a"; "b"; "c"|]
[|""; "q"; "z"|]
[|"can"; "z"; "a"|]
Reverse sorted by col. 1
[|"can"; "z"; "a"|]
[|""; "q"; "z"|]
[|"a"; "b"; "c"|]
Sorted by decreasing length
[|"can"; "z"; "a"|]
[|"a"; "b"; "c"|]
[|""; "q"; "z"|]</pre>
 
=={{header|J}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.