Jump to content

Function prototype: Difference between revisions

(→‎{{header|Kotlin}}: Updated example see https://github.com/dkandalov/rosettacode-kotlin for details)
Line 843:
(: two-args (Integer Integer -> Any))
(define (two-args a b) (void))</lang>
 
=={{header|SNOBOL4}}==
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.
 
To begin with, we look at the definition provided [http://rosettacode.org/wiki/Function_definition#SNOBOL4 at the relevant task page]:
 
<lang snobol4> define('multiply(a,b)') :(mul_end)
multiply multiply = a * b :(return)
mul_end
 
* Test
output = multiply(10.1,12.2)
output = multiply(10,12)
end</lang>
 
The key to this is the <code>define()</code> BIF which declares the actual function and the <code>multiply</code> label which is the entry point to the code that is executed. The key is that SNOBOL4 is an almost militantly unstructured language. There is absolutely nothing special about the <code>multiply</code> entry point that distinguishes it from the target of any other branch target. What happens instead is that the <code>define()</code> BIF associates a certain string pattern--the prototype, in effect--with an entry point. The <code>:(mul_end)</code> piece at the end, in fact, exists because were it not present the body of the <code>multiply</code> "function" would be executed: it is a branch to the label <code>mul_end</code>.
 
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.
 
Of course this implies that you can separate the two pieces. Which you can, like this:
 
<lang snobol4> define('multiply(a,b)')
 
*
* Assume lots more code goes here.
*
:(test)
 
*
* MORE CODE!
*
 
multiply multiply = a * b :(return)
 
*
* MORE CODE!
*
 
test
output = multiply(10.1,12.2)
output = multiply(10,12)
end</lang>
 
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.
 
=={{header|zkl}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.