Named parameters: Difference between revisions

m
→‎{{header|Phix}}: added syntax colouring the hard way, phix/basics
(Added Arturo implementation)
m (→‎{{header|Phix}}: added syntax colouring the hard way, phix/basics)
Line 1,193:
 
=={{header|Phix}}==
{{libheader|Phix/basics}}
Phix supports named and optional parameters in a very simple, natural, and intuitive way, erring on the side of caution when faced with any potential ambiguity.<br>
Optional parameters are specified simply by providing a default, any non-defaulted parameters must occur before (to the left of) any defaulted parameters.<br>
Named parameters can be given (when invoking a routine) in any order, but must be grouped together after (to the right of) any non-named parameters.<br>
Note that low-level builtins (those defined using AutoAsm() in psym.e/syminit()) do not support named parameters (maybe one day..), but everything else does.
 
<lang Phix>global function timedelta(atom weeks=0, atom days=0, atom hours=0, atom minutes=0, atom seconds=0, atom milliseconds=0, atom microseconds=0)
<!--<lang Phix>-->
-- can be invoked as:
<span style="color: #008080;">global</span> <span style="color: #008080;">function</span> <span style="color: #7060A8;">timedelta</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">weeks</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">days</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">hours</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">minutes</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">seconds</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">milliseconds</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">microseconds</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
constant fourdays = timedelta(days:=4)
<span style="color: #000080;font-style:italic;">-- can be invoked as:</span>
-- fourdays = timedelta(0,4) -- equivalent
<span style="color: #008080;">constant</span> <span style="color: #000000;">fourdays</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">timedelta</span><span style="color: #0000FF;">(</span><span style="color: #000000;">days</span><span style="color: #0000FF;">:=</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span>
-- **NB** a plain '=' is a very different thing
<span style="color: #000080;font-style:italic;">-- fourdays = timedelta(0,4) -- equivalent
constant oneday = timedelta(days=1) -- equivalent to timedelta([weeks:=]iff((equal(days,1)?true:false))
-- **NB** a plain '=' is a very different thing</span>
-- - with an error if no local variable days exists.
<span style="color: #008080;">constant</span> <span style="color: #000000;">oneday</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">timedelta</span><span style="color: #0000FF;">(</span><span style="color: #000000;">days</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- equivalent to timedelta([weeks:=]iff((equal(days,1)?true:false))
constant shift = timedelta(hours:=hours) -- perfectly valid (param hours:=local hours)
-- - with an error if no local variable days exists.</span>
-- timedelta(0,hours:=15,3) -- illegal (it is not clear whether you meant days:=3 or minutes:=3)</lang>
<span style="color: #008080;">constant</span> <span style="color: #000000;">shift</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">timedelta</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hours</span><span style="color: #0000FF;">:=</span><span style="color: #000000;">hours</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- perfectly valid (param hours:=local hours)
-- timedelta(0,hours:=15,3) -- illegal (it is not clear whether you meant days:=3 or minutes:=3)</langspan>
<!--</lang>-->
 
=={{header|PHP}}==
7,794

edits