Function prototype: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Changed to Wren S/H)
(5 intermediate revisions by 4 users not shown)
Line 347:
=={{header|D}}==
Beside function prototypes similar to the ones available in C (plus templates, D-style varargs), you can define class method prototypes in abstract classes and interfaces. The exact rules for this are best explained by the [//http://dlang.org/interface.html documentation]
<syntaxhighlight lang="d">/// Declare a function with no arguments that returns an integer.
/// Declare a function with no arguments that returns an integer.
int noArgs();
 
/// Declare a function with notwo arguments that returns an integer.
int twoArgs(int a, int b);
 
Line 858 ⟶ 859:
0 3 6 9 12
0 4 8 12 16</syntaxhighlight>
=={{header|Java}}==
The order of declarations in Java is unimportant, so forward declaration of functions is neither needed nor supported.
The only place where function prototypes are needed is in abstract classes or interfaces, where implementation will be provided by the derived or implementing class.
<syntaxhighlight lang="java">
public final class FunctionPrototype {
public static void main(String[] aArgs) {
Rectangle rectangle = new Rectangle(10.0, 20.0);
System.out.println("Area = " + rectangle.area());
Calculator calculator = new Calculator();
System.out.println("Sum = " + calculator.sum(2, 2));
calculator.version();
}
 
private static class Rectangle implements Shape {
public Rectangle(double aWidth, double aLength) {
width = aWidth; length = aLength;
}
@Override
public double area() {
return length * width;
}
private final double width, length;
}
private static class Calculator extends Arithmetic {
public Calculator() {
// Statements to create the graphical
// representation of the calculator
}
@Override
public int sum(int aOne, int aTwo) {
return aOne + aTwo;
}
@Override
public void version() {
System.out.println("0.0.1");
}
}
 
}
 
interface Shape {
public double area();
}
 
abstract class Arithmetic {
public abstract int sum(int aOne, int aTwo);
public abstract void version();
}
</syntaxhighlight>
{{ out }}
<pre>
Area = 200.0
Sum = 4
0.0.1
</pre>
 
=={{header|JavaScript}}==
Line 942 ⟶ 1,009:
l.pop(); // 15
l.length; // 2
</syntaxhighlight>
 
=={{header|jq}}==
jq does not have "function prototypes" in the strict sense, so this entry focuses on jq function signatures
as specified in function definitions.
 
jq does not limit the number of formal parameters of a function and supports multi-arity functions, but each allowed
"restriction" to a particular arity must be specified explicitly.
 
Although jq does not support varargs functions, their effect can be
achieved by using an array-valued argument in conjunction with
"destructuring", as illustrated below.
 
Note also that:
* any restrictions on the allowed values of the parameters must be specified programmatically and are only checked at run-time;
* function definitions may be included within function definitions;
* recursive and mutually recursive functions are allowed, but calls to a function can only occur within the scope of its definition.
* a function of a particular arity can be defined more than once, with lexical scoping rules determining how each invocation will be handled.
 
In the following examples, only `def` and `as` are jq keywords, and .... is used to indicate ellipsis.
 
<syntaxhighlight lang=jq>
def Func: # no arguments
 
def Func(a;b): # two arguments
 
def Vararg(v):
v as [$a1, $a2] .... # if v is an array, then $a1 will be the first item specified by v, or else null, and so on
 
def Vararg(a; v):
v as [$a1, $a2] .... # if v is an array, then $a1 will be the first item specified by v, or else null, and so on
</syntaxhighlight>
 
Line 1,675 ⟶ 1,773:
 
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.
<syntaxhighlight lang="ecmascriptwren">var factorial // forward declaration
 
factorial = Fn.new { |n| (n <= 1) ? 1 : factorial.call(n-1) * n }
9,476

edits