Function prototype: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|jq}}: Corrected syntax highlighting.)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(2 intermediate revisions by 2 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 1,706 ⟶ 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