Jump to content

Function prototype: Difference between revisions

m
→‎{{header|Phix}}: added syntax colouring the hard way, phix/basics
(Added Delphi example)
m (→‎{{header|Phix}}: added syntax colouring the hard way, phix/basics)
Line 1,186:
 
=={{header|Phix}}==
{{libheader|Phix/basics}}
AsUse explicit forward definitions. Optional, and optional - unless (egfor instance) you want to use named parameters in a forward call.<br>
Should be identical to the actual definition, but preceded by "forward" and with no body.
<!--<lang Phix>-->
<lang Phix>forward function noargs() -- Declare a function with no arguments
<span style="color: #008080;">forward</span> <span style="color: #008080;">function</span> <span style="color: #000000;">noargs</span><span style="color: #0000FF;">()</span> <span style="color: #000080;font-style:italic;">-- Declare a function with no arguments</span>
forward procedure twoargs(integer a, integer b) -- Declare a procedure with two arguments
<span style="color: #008080;">forward</span> <span style="color: #008080;">procedure</span> <span style="color: #000000;">twoargs</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- Declare a procedure with two arguments</span>
forward procedure twoargs(integer, integer /*b*/) -- Parameter names are optional in forward (and actual) definitions
<span style="color: #008080;">forward</span> <span style="color: #008080;">procedure</span> <span style="color: #000000;">twoargs</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000080;font-style:italic;">/*b*/</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- Parameter names are optional in forward (and actual) definitions</span>
forward function anyargs(sequence s) -- varargs are [best/often] handled as a (single) sequence in Phix
<span style="color: #008080;">forward</span> <span style="color: #008080;">function</span> <span style="color: #000000;">anyargs</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- varargs are [best/often] handled as a (single) sequence in Phix</span>
forward function atleastonearg(integer a, integer b=1, ...); -- Default makes args optional (== actual defn)</lang>
<span style="color: #008080;">forward</span> <span style="color: #008080;">function</span> <span style="color: #000000;">atleastonearg</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">...);</span> <span style="color: #000080;font-style:italic;">-- Default makes args optional (== actual defn)</span>
<!--</lang>-->
No special syntax is needed on actual or forward function definitions for named parameters, and calls are identical whether still forward or now fully defined.<br>
Defaults on optional parameters in forward definitions can also be dummy (but type compatible) values should the actual not yet be defined.
7,806

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.