Function prototype: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (Incorrect description: changed from 'no arguments' to 'two arguments')
m (→‎{{header|Wren}}: Changed to Wren S/H)
(One intermediate revision by one other user not shown)
Line 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 1,707 ⟶ 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