Jump to content

Optional parameters: Difference between revisions

add E example
(→‎{{header|Python}}: See the Python entry in Named Arguments for a more comprehensive description of Python function parameters and call arguments.)
(add E example)
Line 109:
CL-USER> (sort-table *data* :ordering (lambda (a b) (> (length a) (length b))))
(("zap" "zip" "Zot") ("a" "b" "c") ("" "q" "z"))</lang>
 
=={{header|E}}==
 
In E, as in Java and Smalltalk, optional parameters are defined as different methods with the same base name. Methods are distinguished by name (''verb'') and number of parameters (''arity'').
 
<lang e>def defaultOrdering(a, b) { return a.op__cmp(b) }
 
def sort {
 
to run(table) {
return sort(table, 0, false, defaultOrdering)
}
to run(table, column) {
return sort(table, column, false, defaultOrdering)
}
to run(table, column, reverse) {
return sort(table, column, reverse, defaultOrdering)
}
 
to run(table :List[List[String]], column :int, reverse :boolean, ordering) {
return table.sort(fn a, b {
def ord := ordering(a[column], b[column])
if (reverse) { -ord } else { ord }
})
}
 
}</lang>
 
Named parameters are not builtin, but map-patterns may be used as a substitute. (TODO: Example of this) [[Category:E examples needing attention]]
 
=={{header|Fortran}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.