Function prototype: Difference between revisions

Content added Content deleted
m (Oops. Screwed up the wiki formatting.)
(→‎{{header|SNOBOL4}}: Added explanatory section headers and clarified the example.)
Line 847: Line 847:
In SNOBOL4, functions are actually a hack and are defined in an idiosyncratic way that is simultaneously like a prototype or not like one as the case may be.
In SNOBOL4, functions are actually a hack and are defined in an idiosyncratic way that is simultaneously like a prototype or not like one as the case may be.


===Basics===
To begin with, we look at the definition provided [http://rosettacode.org/wiki/Function_definition#SNOBOL4 at the relevant task page]:
To begin with, we look at the definition provided [http://rosettacode.org/wiki/Function_definition#SNOBOL4 at the relevant task page]:


Line 861: Line 862:


On execution, the SNOBOL4 runtime will execute line by line of the script. When it reaches the <code>define</code> BIF call it will do the stuff it needs to do behind the scenes to set up function-like access to the <code>multiply</code> branch target. It would then proceed to execute the next line were it not for the branch.
On execution, the SNOBOL4 runtime will execute line by line of the script. When it reaches the <code>define</code> BIF call it will do the stuff it needs to do behind the scenes to set up function-like access to the <code>multiply</code> branch target. It would then proceed to execute the next line were it not for the branch.

===Separation of prototype and body===


Of course this implies that you can separate the two pieces. Which you can, like this:
Of course this implies that you can separate the two pieces. Which you can, like this:
Line 887: Line 890:


With this structure the "function" is declared at the program, the implementation is somewhere down in the middle, and the mainline (<code>test</code> here) is at the end.
With this structure the "function" is declared at the program, the implementation is somewhere down in the middle, and the mainline (<code>test</code> here) is at the end.

===Full prototyping===


The <code>define()</code> BIF is used for more than merely providing function-like access to a label with the same name. It is used to prototype all of these (with some default behaviour):
The <code>define()</code> BIF is used for more than merely providing function-like access to a label with the same name. It is used to prototype all of these (with some default behaviour):
Line 892: Line 897:
* the function name (<code>multiply</code> in the examples);
* the function name (<code>multiply</code> in the examples);
* the formal arguments to the function (<code>a, b</code> in the examples);
* the formal arguments to the function (<code>a, b</code> in the examples);
* the entry point label for the function's code (defaults to the function name);
* the entry point label for the function's code (defaults to the function name, <code>mult_impl</code> in the following example);
* any local variables which should be protected in the function (defaults to none).
* any local variables which should be protected in the function (defaults to none, <code>acc1,acc2</code> in the following example).


Thus a highly-contrived example function that illustrates all of these would look like this:
Thus a highly-contrived example function that illustrates all of these would look like this: