Function prototype: Difference between revisions
Content added Content deleted
m (→{{header|zkl}}: deleted duplicated word) |
(Added Wren) |
||
Line 1,281: | Line 1,281: | ||
output = multiply(10,12) |
output = multiply(10,12) |
||
end</lang> |
end</lang> |
||
=={{header|Wren}}== |
|||
Firstly, Wren makes a distinction between functions and methods. The latter are always members of a class and never need to be prototyped regardless of the order in which they are declared or called. |
|||
On the other hand functions are standalone objects and cannot be called before they have been declared. Consequently, prototypes are required if a function calls itself recursively, if two (or more) functions are mutually recursive or if a function is simply called out of order for some reason. |
|||
A prototype is just a forward declaration of the function's name. Details of any parameters are not needed and cannot even be optionally specified as parameters are considered to be part of the function's body. |
|||
In the following example, the 'factorial' function is recursive and so needs a forward declaration. However, even though the function takes a single argument, no prior information about that is needed or possible. There is an example of mutual recursion protoyping in the [[Mutual_recursion#Wren]] task. |
|||
<lang ecmascript>var factorial // forward declaration |
|||
factorial = Fn.new { |n| (n <= 1) ? 1 : factorial.call(n-1) * n } |
|||
System.print(factorial.call(5))</lang> |
|||
{{out}} |
|||
<pre> |
|||
120 |
|||
</pre> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |