Function prototype: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(29 intermediate revisions by 19 users not shown)
Line 1:
{{task}}
{{omit from|6502 Assembly}}
{{omit from|68000 Assembly}}
{{omit from|Z80 Assembly}}
Some languages provide the facility to declare functions and subroutines through the use of [[wp:Function prototype|function prototyping]].
 
Line 25 ⟶ 28:
* For a main program, specifications are only necessary if a function call appears in the source before the function definition.
* For a package, specifications must appear as part of the specification(.ads) file, and do not appear in the body file(.adb) (The file extensions apply to Gnat Ada and may not apply to all compilers).
<langsyntaxhighlight Adalang="ada">function noargs return Integer;
function twoargs (a, b : Integer) return Integer;
-- varargs do not exist
function optionalargs (a, b : Integer := 0) return Integer;
-- all parameters are always named, only calling by name differs
procedure dostuff (a : Integer);</langsyntaxhighlight>
Other Prototyping: Since pointers are not generic in Ada, a type must be defined before one can have a pointer to that type, thus for making linked-list type semantics another trivial prototyping exists:
<langsyntaxhighlight Adalang="ada">type Box; -- tell Ada a box exists (undefined yet)
type accBox is access Box; -- define a pointer to a box
type Box is record -- later define what a box is
next : accBox; -- including that a box holds access to other boxes
end record;</langsyntaxhighlight>
Example of a package specification (i.e. prototype):
<langsyntaxhighlight Adalang="ada">package Stack is
procedure Push(Object:Integer);
function Pull return Integer;
end Stack;</langsyntaxhighlight>
 
Example of a package body:
<langsyntaxhighlight Adalang="ada">package body Stack is
 
procedure Push(Object:Integer) is
Line 56 ⟶ 59:
end;
 
end Stack;</langsyntaxhighlight>
 
To use the package and function:
<langsyntaxhighlight Adalang="ada">with Stack;
procedure Main is
N:integer:=5;
Line 66 ⟶ 69:
...
N := Pull;
end Main;</langsyntaxhighlight>
 
=={{header|Aime}}==
<syntaxhighlight lang="aime">integer f0(void); # No arguments
void f1(integer, real); # Two arguments
real f2(...); # Varargs
void f3(integer, ...); # Varargs
 
void f4(integer &, text &); # Two arguments (integer and string), pass by reference
integer f5(integer, integer (*)(integer));
# Two arguments: integer and function returning integer and taking one integer argument
integer f6(integer a, real b); # Parameters names are allowed
record f7(void); # Function returning an associative array</syntaxhighlight>
 
=={{header|ALGOL 68}}==
Line 72 ⟶ 87:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-2.8 algol68g-2.8].}}
{{works with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d]}}
'''File: Function_prototype.a68'''<langsyntaxhighlight lang="algol68">#!/usr/bin/a68g --script #
# -*- coding: utf-8 -*- #
 
Line 108 ⟶ 123:
END COMMENT
 
SKIP</langsyntaxhighlight>
 
=={{header|AimeAmazing Hopper}}==
<lang aime>integer f0(void); # No arguments
void f1(integer, real); # Two arguments
real f2(...); # Varargs
void f3(integer, ...); # Varargs
 
Function prototypes are included in a #PROTO declaration in the header, at the beginning of a HOPPER source file, before the functional code (MAIN :). This is enforced by the HOPPER compiler, to declare pseudo functions.
void f4(integer &, text &); # Two arguments (integer and string), pass by reference
 
integer f5(integer, integer (*)(integer));
<syntaxhighlight lang="amazing hopper">
# Two arguments: integer and function returning integer and taking one integer argument
#!/usr/bin/hopper
integer f6(integer a, real b); # Parameters names are allowed
 
record f7(void); # Function returning an associative array</lang>
// Archivo Hopper
#include <hopper.h>
 
#context-free noargs /* Declare a pseudo-function with no argument */
#synon noargs no arguments
#context multiargs /* Declare a pseudo-function with multi arguments */
#proto twoargs(_X_,_Y_) /* Declare a pseudo-function with two arguments. #PROTO need arguments */
 
main:
no arguments
_two args(2,2) // pseudo-function #proto need "_" sufix
println
{1,2,3,"hola mundo!","\n"}, multiargs
exit(0)
 
.locals
 
multiargs:
_PARAMS_={},pushall(_PARAMS_)
[1:3]get(_PARAMS_),stats(SUMMATORY),println
{"Mensaje: "}[4:5]get(_PARAMS_),println
clear(_PARAMS_)
back
 
twoargs(a,b)
{a}mulby(b)
back
 
// This function is as useful a s an ashtray on a motorcycle:
no args:
{0}minus(0),kill
back
 
{0}return
</syntaxhighlight>
 
=={{header|C}}==
Line 126 ⟶ 172:
Function prototypes are typically included in a header file at the beginning of a source file prior to functional code. However, this is not enforced by a compiler.
 
<langsyntaxhighlight lang="c">int noargs(void); /* Declare a function with no argument that returns an integer */
int twoargs(int a,int b); /* Declare a function with two arguments that returns an integer */
int twoargs(int ,int); /* Parameter names are optional in a prototype definition */
int anyargs(); /* An empty parameter list can be used to declare a function that accepts varargs */
int atleastoneargs(int, ...); /* One mandatory integer argument followed by varargs */</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
'''Abstract methods'''<br/>
Interfaces and abstract classes can define abstract methods that must be implemented by subclasses.
<langsyntaxhighlight lang="csharp">using System;
abstract class Printer
{
Line 146 ⟶ 192:
Console.WriteLine("Hello world!");
}
}</langsyntaxhighlight>
'''Delegates'''<br/>
A delegate is similar to a function pointer. They are multicast: multiple methods can be attached to them.
<langsyntaxhighlight lang="csharp">using System;
public delegate int IntFunction(int a, int b);
 
Line 170 ⟶ 216:
Console.WriteLine(func(2, 3)); //prints 5. Both functions are called, but only the last result is kept.
}
}</langsyntaxhighlight>
'''Partial methods'''<br/>
A partial type is a type that is defined in multiple files.<br/>
Line 178 ⟶ 224:
- The method must return void.<br/>
- No access modifiers are allowed. Partial methods are implicitly private.
<langsyntaxhighlight lang="csharp">//file1.cs
public partial class Program
{
Line 197 ⟶ 243:
p.Print(); //If the implementation above is not written, the compiler will remove this line.
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
Function declaration in C++ differs from that in C in some aspect.
 
<langsyntaxhighlight lang="cpp">int noargs(); // Declare a function with no arguments that returns an integer
int twoargs(int a,int b); // Declare a function with two arguments that returns an integer
int twoargs(int ,int); // Parameter names are optional in a prototype definition
Line 209 ⟶ 255:
template<typename T> T declval(T); //A function template
template<typename ...T> tuple<T...> make_tuple(T...); //Function template using parameter pack (since c++11)
</syntaxhighlight>
</lang>
 
=={{header|Clojure}}==
If you want to make forward declarations, you can use declare.
<syntaxhighlight lang ="clojure">(declare foo)</langsyntaxhighlight>
 
=={{header|COBOL}}==
Line 219 ⟶ 265:
Prototypes were introduced in COBOL 2002. In the following examples, <code>PROGRAM-ID</code> and <code>PROGRAM</code> can be replaced with the equivalents for functions and methods. However, in method prototypes the <code>PROTOTYPE</code> clause is not used.
 
<langsyntaxhighlight lang="cobol"> *> A subprogram taking no arguments and returning nothing.
PROGRAM-ID. no-args PROTOTYPE.
END PROGRAM no-args.
Line 257 ⟶ 303:
01 ret PIC S9(9) USAGE COMP-5.
PROCEDURE DIVISION USING arg RETURNING ret.
END PROGRAM foreign-func.</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
In Common Lisp, function prototypes can be used with <code>(declaim (inline func-name))</code> function arguments are taken when the function is defined. In addition, the argument types aren't needed.
 
Caveat -- This works with specific implementations of CL. This was tested in SBCL.
<lang lisp>
 
<syntaxhighlight lang="lisp">
(declaim (inline no-args))
(declaim (inline one-arg))
Line 293 ⟶ 341:
 
(optional-args 1.0 "example") ;=> "1.0 example"
</syntaxhighlight>
</lang>
 
[http://clhs.lisp.se/Body/m_declai.htm More about <code>declaim</code> here]
Line 299 ⟶ 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">
<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 345 ⟶ 394:
}
 
void main() {}</langsyntaxhighlight>
 
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
In Delphi, prototype function is named Class/Record Helper. For now, is not possible has more then one helper active in a same object, if two or more was declareted, just the present in the last Unit declareted will be active, the others will be ignored. In case two or more helpers was declareted in same Unit, just the last helper declareted will be active. Can not inherit record helpers, but class helper can be.<br>
Patten: " identifierName = record helper for TypeIdentifierName"<br>
See [http://docwiki.embarcadero.com/RADStudio/Sydney/en/Class_and_Record_Helpers_(Delphi) Documentation] for more details.<br>
 
<syntaxhighlight lang="delphi">
program Function_prototype;
 
{$APPTYPE CONSOLE}
 
uses
System.SysUtils;
 
type
TIntArray = TArray<Integer>;
 
TIntArrayHelper = record helper for TIntArray
const
DEFAULT_VALUE = -1;
// A prototype declaration for a function that does not require arguments
function ToString(): string;
 
// A prototype declaration for a function that requires two arguments
procedure Insert(Index: Integer; value: Integer);
 
// A prototype declaration for a function that utilizes varargs
// varargs is not available, but a equivalent is array of const
procedure From(Args: array of const);
 
//A prototype declaration for a function that utilizes optional arguments
procedure Delete(Index: Integer; Count: Integer = 1);
 
//A prototype declaration for a function that utilizes named parameters
// Named parameters is not supported in Delphi
 
//Example of prototype declarations for subroutines or procedures
//(if these differ from functions)
procedure Sqr; //Procedure return nothing
function Averange: double; //Function return a value
end;
 
{ TIntHelper }
 
function TIntArrayHelper.Averange: double;
begin
Result := 0;
for var e in self do
Result := Result + e;
Result := Result / Length(self);
end;
 
procedure TIntArrayHelper.Delete(Index, Count: Integer);
begin
System.Delete(self, Index, Count);
end;
 
procedure TIntArrayHelper.From(Args: array of const);
var
I, Count: Integer;
begin
Count := Length(Args);
SetLength(self, Count);
 
if Count = 0 then
exit;
for I := 0 to High(Args) do
with Args[I] do
case VType of
vtInteger:
self[I] := VInteger;
vtBoolean:
self[I] := ord(VBoolean);
vtChar, vtWideChar:
self[I] := StrToIntDef(string(VChar), DEFAULT_VALUE);
vtExtended:
self[I] := Round(VExtended^);
vtString:
self[I] := StrToIntDef(VString^, DEFAULT_VALUE);
vtPChar:
self[I] := StrToIntDef(VPChar, DEFAULT_VALUE);
vtObject:
self[I] := cardinal(VObject);
vtClass:
self[I] := cardinal(VClass);
vtAnsiString:
self[I] := StrToIntDef(string(VAnsiString), DEFAULT_VALUE);
vtCurrency:
self[I] := Round(VCurrency^);
vtVariant:
self[I] := Integer(VVariant^);
vtInt64:
self[I] := Integer(VInt64^);
vtUnicodeString:
self[I] := StrToIntDef(string(VUnicodeString), DEFAULT_VALUE);
end;
end;
 
procedure TIntArrayHelper.Insert(Index, value: Integer);
begin
system.Insert([value], self, Index);
end;
 
procedure TIntArrayHelper.Sqr;
begin
for var I := 0 to High(self) do
Self[I] := Self[I] * Self[I];
end;
 
function TIntArrayHelper.ToString: string;
begin
Result := '[';
for var e in self do
Result := Result + e.ToString + ', ';
Result := Result + ']';
end;
 
begin
var val: TArray<Integer>;
val.From([1, '2', PI]);
val.Insert(0, -1); // insert -1 at position 0
writeln(' Array: ', val.ToString, ' ');
writeln(' Averange: ', val.Averange: 3: 2);
val.Sqr;
writeln(' Sqr: ', val.ToString);
Readln;
 
end.</syntaxhighlight>
{{out}}
<pre> Array: [-1, 1, 2, 3, ]
Averange: 1.25
Sqr: [1, 1, 4, 9, ]</pre>
 
Class helper example, with inherited helpers:<br>
Patten: " identifierName = class helper (ancestor list*) for TypeIdentifierName"<br>
Ancertor list is optional, and only will be used if this helper will inhret a parent helper.
 
{{libheader| System.SysUtils}}
{{libheader| System.Classes}}
<syntaxhighlight lang="delphi">
program Function_prototype_class;
 
{$APPTYPE CONSOLE}
 
uses
System.SysUtils,
System.Classes;
 
type
TStringListHelper1 = class helper for TStringList
constructor Create(FileName: TFileName); overload;
end;
 
TStringListHelper2 = class helper (TStringListHelper1) for TStringList
procedure SaveAndFree(FileName: TFileName);
end;
 
TStringListHelper3 = class helper (TStringListHelper2) for TStringList
procedure AddDateTime;
end;
 
{ TStringListHelper1 }
 
constructor TStringListHelper1.Create(FileName: TFileName);
begin
inherited Create;
if FileExists(FileName) then
LoadFromFile(FileName);
end;
 
{ TStringListHelper2 }
 
procedure TStringListHelper2.SaveAndFree(FileName: TFileName);
begin
SaveToFile(FileName);
Free;
end;
 
{ TStringListHelper3 }
 
procedure TStringListHelper3.AddDateTime;
begin
self.Add(DateToStr(now));
end;
 
begin
with TStringList.Create('d:\Text.txt') do
begin
AddDateTime;
SaveAndFree('d:\Text_done.txt');
end;
readln;
end.</syntaxhighlight>
 
=={{header|Diego}}==
<syntaxhighlight lang="diego">// A prototype declaration for a function that does not require arguments
 
begin_funct(foo); // function as a 'method' with no arguments, no return type
end_funct(foo);
 
// or
 
with_funct(foo); // function as a 'method' with no arguments, no return type
 
// A prototype declaration for a function that requires two arguments
 
begin_funct(goo)_arg({string}, str1, str2); // two arguments, no return type
end_funct[]; // derived name of function using [], like 'this'
 
with_funct(goo)_arg({str}, str1, str2); // two arguments, no return type
with_funct(hoo)_param({integer}, i, j); // 'param' posit can be used instead of 'arg'
 
// A prototype declaration for a function that utilizes varargs
 
begin_funct(voo)_arg({int}, [vararg], v); // variable number of arguments, no return type, 'int' can be used instead of 'integer'
end_funct[];
 
begin_funct({int}, voo)_arg({int}, ..., v); // variable number of arguments, with return type
add_var({int}, sum)_v(0);
forall_var(v)_calc([sum]+=[v]);
[voo]_ret([sum]);
end_funct[];
 
// A prototype declaration for a function that utilizes optional arguments
begin_funct({int}, ooo)_arg(o)_value(1); // optional argument with default value and return type integer
with_funct(ooo)_return([o]); // Can be shortened to [ooo]_ret([0]);
end_funct[];
 
begin_funct({int}, oxo)_arg(o,u,v)_opt(u)_value(1); // optional argument of second argument with default value and return type integer
[ooo]_ret(1); // the execution has to name arguments or missing in comma-separated list of arguments
end_funct[];
 
// A prototype declaration for a function that utilizes named parameters
 
begin_funct({int}, poo)_param({int}, a, b, c); // to enforce named parameters '_param' posit can be used.
[poo]_ret([a]+[b]+[c]);
end_funct[];
 
exec_funct(poo)_param(a)_value(1)_param(b, c)_value(2, 3) ? me_msg()_funct(poo); ;
 
begin_funct({int}, poo)_arg({int}, a, b, c); // named parameters can still be used with '_arg' posit.
[poo]_ret([a]+[b]+[c]);
end_funct[];
 
me_msg()_funct(poo)_arg(a)_value(1)_value(2, 3); // Callee has to figure out unnamed arguments by extraction
// 'exec_' verb is implied before '_funct' action
 
// Example of prototype declarations for subroutines or procedures (if these differ from functions)
 
begin_instruct(foo); // instructions are 'methods', no arguments, no return type
end_instruct[foo]; // explicit end of itself
 
// or
 
with_instruct(foo); // instructions are 'methods', no arguments, no return type
 
begin_funct(yoo)_arg(robotMoniker)_param(b); // A '_funct' can be used as a subroutine when missing the '{}' return datatype
// a mix of '_arg' and '_param' posits can be used
with_robot[robotMoniker]_var(sq)_calc([b]^2); // create a variable called 'sq' on robot 'robotMoniker'
end_funct(yoo);
 
begin_instruct(woo)_arg(robotType)_param(b); // An '_instuct' is only used for subroutines and return datatypes are not accepted
with_robot()_type[robotType]_var(sq)_calc([b]^2); // create a variable called 'sq' on all robots of type 'robotType'
end_funct(woo);
 
// An explanation and example of any special forms of prototyping not covered by the above
 
begin_funct({double}, voo)_arg({int}, [numArgs], v); // variable-defined number of arguments, with return type
add_var({int}, sum)_v(0);
add_var({double}, average)_v(0);
for_var(v)_until[numArgs]_calc([sum]+=[v]); // the number of arguments [numArgs] does not have to be number of arguments of v
[voo]_ret([sum]/[numArgs]);
end_funct[];
 
begin_funct({int}, [numArgsOut], voo)_arg({int}, [numArgsIn], v); // variable-defined number of arguments, with variable-defined number of return types
add_var({int}, sum)_v(0);
add_var({double}, average)_v(0);
for_var(v)_until[numArgsOut]_calc([sum]+=[v]);
[voo]_ret([sum]/[numArgsOut]);
end_funct[];</syntaxhighlight>
 
=={{header|F Sharp|F#}}==
In F#, prototypes are called signatures. Signature files are used to bulk annotate the accessibility of the things within them. If something is in an implementation file but not in the signature file, it is assumed to be private to that file. If it is in the signature file without the <code>internal</code> accessibility modifier, then it is assumed to public, otherwise it is internal to the assembly. For more details, see the [http://msdn.microsoft.com/en-us/library/dd233196.aspx documentation]. Below is an example of the signatures produced for the functions specified in the task (without any accessibility modifiers):
<syntaxhighlight lang="fsharp">// A function taking and returning nothing (unit).
val noArgs : unit -> unit
// A function taking two integers, and returning an integer.
val twoArgs : int -> int -> int
// A function taking a ParamPack array of ints, and returning an int. The ParamPack
// attribute is not included in the signature.
val varArgs : int [] -> int
// A function taking an int and a ParamPack array of ints, and returning an
// object of the same type.
val atLeastOnArg : int -> int [] -> int
// A function taking an int Option, and returning an int.
val optionalArg : Option<int> -> int
 
// Named arguments and the other form of optional arguments are only available on
// methods.
type methodClass =
class
// A method taking an int named x, and returning an int.
member NamedArg : x:int -> int
// A method taking two optional ints in a tuple, and returning an int. The
//optional arguments must be tupled.
member OptionalArgs : ?x:int * ?y:int -> int
end</syntaxhighlight>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
' The position regarding prototypes is broadly similar to that of the C language in that functions,
Line 384 ⟶ 741:
Private:
i As Integer
End Type</langsyntaxhighlight>
 
=={{header|F Sharp|F#}}==
In F#, prototypes are called signatures. Signature files are used to bulk annotate the accessibility of the things within them. If something is in an implementation file but not in the signature file, it is assumed to be private to that file. If it is in the signature file without the <code>internal</code> accessibility modifier, then it is assumed to public, otherwise it is internal to the assembly. For more details, see the [http://msdn.microsoft.com/en-us/library/dd233196.aspx documentation]. Below is an example of the signatures produced for the functions specified in the task (without any accessibility modifiers):
<lang fsharp>// A function taking and returning nothing (unit).
val noArgs : unit -> unit
// A function taking two integers, and returning an integer.
val twoArgs : int -> int -> int
// A function taking a ParamPack array of ints, and returning an int. The ParamPack
// attribute is not included in the signature.
val varArgs : int [] -> int
// A function taking an int and a ParamPack array of ints, and returning an
// object of the same type.
val atLeastOnArg : int -> int [] -> int
// A function taking an int Option, and returning an int.
val optionalArg : Option<int> -> int
 
// Named arguments and the other form of optional arguments are only available on
// methods.
type methodClass =
class
// A method taking an int named x, and returning an int.
member NamedArg : x:int -> int
// A method taking two optional ints in a tuple, and returning an int. The
//optional arguments must be tupled.
member OptionalArgs : ?x:int * ?y:int -> int
end</lang>
 
=={{header|Go}}==
Line 416 ⟶ 747:
 
Function declarations whether with a body or without must be "top level" declarations, that is, after the package clause and outside of any other function. Examples of function delarations without bodies are,
<langsyntaxhighlight lang="go">func a() // function with no arguments
func b(x, y int) // function with two arguments
func c(...int) // varargs are called "variadic parameters" in Go.</langsyntaxhighlight>
Go does not directly support optional or named parameters and does not have any concept of procedures or subroutines as distinct from functions.
 
Otherwise, Go does have the concept of a function signature which includes parameters and return values. Go is strongly typed and functions are first class objects so function signatures are used in a variety of ways. These might be considered distinct from the concept of function prototype.
 
=={{header| haskell Haskell}}==
A function can be declared without giving it's prototype in Haskell. The haskell compiler has got type inference
whereby it can infer the return type and type of variable given to function. You can still hardcode the prototype
which specifies the datatype of variables and return type. For ex. Consider a function add which takes two
integers and returns their sum. It can be prototyped and declared as :
<langsyntaxhighlight lang="haskell">
add :: Int -> Int -> Int
add x y = x+y
</syntaxhighlight>
</lang>
 
Actually all functions in haskell are functions with just one arguments. Haskell will treat above function as a
Line 437 ⟶ 768:
is such that it takes an int and returns an int.
Similarly for any function add which takes 3 integers and adds them the actual prototype will be as follows:
<langsyntaxhighlight lang="haskell">
add :: Int->(Int ->(Int->Int))
</syntaxhighlight>
</lang>
The one that does not require arguements could just be:
<langsyntaxhighlight lang="haskell">
printThis = putStrLn("This is being printed.")
</syntaxhighlight>
</lang>
But haskell would rather consider the function to be of return type IO() in this case.
 
Two arguments:
<langsyntaxhighlight lang="haskell">
add :: Int -> Int -> Int
add x y = x+y
</syntaxhighlight>
</lang>
 
The same thing can be done using the lambda function as :
<langsyntaxhighlight lang="haskell">
add :: Int -> Int -> Int
add = \x->\y -> x+y
</syntaxhighlight>
</lang>
 
Two arguments with unnamed parameters:
<langsyntaxhighlight lang="haskell">
doThis :: Int-> Int-> String
doThis _ _ = "Function with unnamed parameters"
</syntaxhighlight>
</lang>
 
Function with var args requires creation of type class as per the requirement.
Line 468 ⟶ 799:
=={{header|J}}==
J assumes an unknown name is a verb of infinite rank. Rank determines the frames on which the verb executes. As the demonstration shows, changing the rank, assigning the rank before the verb is used in other definitions affects the result. We could, of course, play other games by changing the unknown name from verb to another part of speech.
<langsyntaxhighlight Jlang="j"> NB. j assumes an unknown name f is a verb of infinite rank
NB. f has infinite ranks
f b. 0
Line 527 ⟶ 858:
0 2 4 6 8
0 3 6 9 12
0 4 8 12 16</langsyntaxhighlight>
=={{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}}==
=== ES5 ===
JavaScript functions may also be used to define prototype objects
<syntaxhighlight lang="javascript">
<lang JavaScript>
// A prototype declaration for a function that does not require arguments
function List() {}
Line 570 ⟶ 967:
l.length; // 2
 
</syntaxhighlight>
</lang>
 
=== ES6 ===
Class Declarations are used to define prototype objects
<syntaxhighlight lang="javascript">
<lang JavaScript>
// A prototype declaration for a function that does not require arguments
class List {
Line 612 ⟶ 1,009:
l.pop(); // 15
l.length; // 2
</syntaxhighlight>
</lang>
 
=={{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>
 
=={{header|Julia}}==
Julia does not need or use function prototypes in general. Generic functions are further specialized as to argument type and return type during just-in-time compilation if required. However, when interacting with other languages such a C which use function prototypes, Julia can prototype its functions for passing its functions to external languages with the @cfunction macro:
 
<syntaxhighlight lang="julia">julia > function mycompare(a, b)::Cint
(a < b) ? -1 : ((a > b) ? +1 : 0)
end
mycompare (generic function with 1 method)
</syntaxhighlight>
 
Using @cfunction to create a prototype for passing this to C's quicksort:
 
<syntaxhighlight lang="julia">julia> mycompare_c = @cfunction(mycompare, Cint, (Ref{Cdouble}, Ref{Cdouble}))
</syntaxhighlight>
 
=={{header|Kotlin}}==
Line 620 ⟶ 1,062:
 
Here's an example of this. Note that since Kotlin allows arguments to be passed either by name or position for all functions, there is no separate prototype for this situation. Moreover, since arguments may be passed by name, it is strongly recommended (but not obligatory) that the parameter names for overriding members should be the same as for the functions they override. The compiler will issue a warning if this recommendation is not followed.
<langsyntaxhighlight lang="scala">// version 1.0.6
 
interface MyInterface {
Line 654 ⟶ 1,096:
println(d.roo())
println(d.poo)
}</langsyntaxhighlight>
 
{{out}}
Line 665 ⟶ 1,107:
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">function Func() -- Does not require arguments
return 1
end
Line 679 ⟶ 1,121:
function Func(a,...) -- One argument followed by varargs
return a,{...} -- Returns both arguments, varargs as table
end</langsyntaxhighlight>
 
=={{header|Luck}}==
<langsyntaxhighlight Lucklang="luck">function noargs(): int = ? ;;
function twoargs(x:int, y:int): int = ? ;;
 
Line 689 ⟶ 1,131:
 
function anyargs(xs: ...): int = ? ;;
function plusargs(x:int, xs: ...): int = ? ;;</langsyntaxhighlight>
 
 
=={{header|M2000 Interpreter}}==
Line 698 ⟶ 1,139:
Example of change an inner module using another module with same signature. Module MyBeta {Read x : ... } or Module MyBeta (x) { ... } or Module MyBeta(x) { } is the same.
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Check {
Module MyBeta (a) {
Line 712 ⟶ 1,153:
}
Check
</syntaxhighlight>
</lang>
 
Signatures needed for Event object. An event object get a list of functions, called as modules, and call every function with same signature. We can provide arguments by reference too. We can define simple functions (without visibility except local and global), or group functions (static groups) with visibility local, global and group level, or we can define local scope functions.
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Check {,
\\ make an event object
Line 761 ⟶ 1,202:
}
Check
</syntaxhighlight>
</lang>
 
Using a function for local call (module visibility)
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Check {,
\\ make an event object
Line 817 ⟶ 1,258:
}
Check
</syntaxhighlight>
</lang>
 
Using a Function in a Group (Groups are the User objects in M2000)
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Check {,
\\ make an event object
Line 872 ⟶ 1,313:
}
Check
</syntaxhighlight>
</lang>
 
 
=={{header|Nim}}==
Procedure declarations can be used if a proc is to be used before its definition.
<langsyntaxhighlight lang="nim"># Procedure declarations. All are named
proc noargs(): int
proc twoargs(a, b: int): int
Line 893 ⟶ 1,333:
proc twoargs(a, b: int): int = echo "twoargs"
proc anyargs(x: varargs[int]): int = echo "anyargs"
proc optargs(a: int, b = 10): int = echo "optargs"</langsyntaxhighlight>
 
=={{header|OCaml}}==
 
<syntaxhighlight lang="ocaml">(* Usually prototype declarations are put in an interface file,
a file with .mli filename extension *)
 
(* A prototype declaration for a function that does not require arguments *)
val no_arg : unit -> unit
 
(* A prototype declaration for a function that requires two arguments *)
val two_args : int -> int -> unit
 
(* A prototype declaration for a function that utilizes optional arguments *)
val opt_arg : ?param:int -> unit -> unit
(* in this case we add a unit parameter in order to omit the argument,
because ocaml supports partial application *)
 
(* A prototype declaration for a function that utilizes named parameters *)
val named_arg : param1:int -> param2:int -> unit
 
(* An explanation and example of any special forms of prototyping not covered by the above *)
 
(* A prototype declaration for a function that requires a function argument *)
val fun_arg : (int -> int) -> unit
 
(* A prototype declaration for a function with polymorphic argument *)
val poly_arg : 'a -> unit</syntaxhighlight>
 
=={{header|Oforth}}==
 
Oforth can only forward declare methods (see Mutual Recursion task). A method can be declared without any class implementation :
<syntaxhighlight lang Oforth="oforth">Method new: myMethod</langsyntaxhighlight>
 
This creates a new method object with name myMethod (or does nothing if this object already exists). It says nothing about method implementations (number of parameters, return value, ...).
Line 914 ⟶ 1,381:
 
Ol functions is a first-class functions with dynamic arguments translation so no function prototypes is required.
 
=={{header|OxygenBasic}}==
<syntaxhighlight lang="text">
'DECLARE FUNCTION' ABBREVIATED TO '!'
 
! f() ' a procedure with no params
! f(int a) ' with 1 int param
! f(int *a) ' with 1 int pointer param
! f(int a, int b, inc c) ' with 3 int params
! f(int a,b,c) ' compaction with 3 int params
! f(string s, int a,b) ' with 1 string and 2 int params
! f() as string ' function returning a string
! f(string s) as string ' with 1 string param
! *f(string s) as string ' as a function pointer: @f=address
! f(string s, optional i) ' with opptional param
! f(string s = "Hello") ' optional param with default value
! f(int n, ...) ' 1 specific param and varargs
! f(...) ' any params or none
 
'TRADITIONAL BASIC DECLARATIONS
declare sub f( s as string, i as long, j as long) ' byref by default
declare function f( byref s as string, byval i as long, byval j as long) as string
 
'C-STYLE DECLARATIONS
void f(string *s, long i, long j)
string f(string *s, long i, long j)
 
 
 
'BLOCK DIRECTIVES FOR FUNCTION PROTOTYPES:
 
extern ' shareable stdcall functions
 
extern lib "xyz.dll" ' for accessing functions in xyz Dynamic Link Library
 
extern export ' functions to be exported if this is a DLL
 
extern virtual ' for accssing interfaces and other virtual classes
 
end extern ' revert to internal function mode
 
</syntaxhighlight>
 
=={{header|PARI/GP}}==
Line 920 ⟶ 1,429:
PARI uses C prototypes. Additionally, gp2c parser codes are essentially function prototypes. They must be placed in the file called by gp2c, not in a file included by it, and they must appear as a <code>GP;</code> comment. For a function
 
<langsyntaxhighlight lang="c">long
foo(GEN a, GEN b)</langsyntaxhighlight>
which takes two (required) <code>t_INT</code> arguments, returns a small integer (a [[C]] long) and appears as <code>bar</code> to the gp interpreter, the following command would be used:
<langsyntaxhighlight lang="c">/*
GP;install("foo","LGG","bar","./filename.gp.so");
*/</langsyntaxhighlight>
 
If its arguments were optional it could be coded as
<langsyntaxhighlight lang="c">/*
GP;install("foo","LDGDG","bar","./filename.gp.so");
*/</langsyntaxhighlight>
although other parser codes are possible; this one sends <code>NULL</code> if the arguments are omitted.
 
A code like
<langsyntaxhighlight lang="c">/*
GP;install("foo","s*","bar","./filename.gp.so");
*/</langsyntaxhighlight>
can be used to take a variable (0 or more) number of arguments. Note that the return type in this case is implicitly <code>GEN</code>
 
Line 945 ⟶ 1,454:
The perl scripting language allows prototypes to be checked during JIT compilation. Prototypes should be placed before subroutine definitions, declarations, or anonymous subroutines. The sigil [[Special characters#Perl|special symbols]] act as argument type placeholders.
 
<langsyntaxhighlight lang="perl">sub noargs(); # Declare a function with no arguments
sub twoargs($$); # Declare a function with two scalar arguments. The two sigils act as argument type placeholders
sub noargs :prototype(); # Using the :attribute syntax instead
sub twoargs :prototype($$);</langsyntaxhighlight>
 
=={{header|Perl 6Phix}}==
{{libheader|Phix/basics}}
There is no restriction on placement of prototype declarations. (Actually, we call them "stub declarations".) In fact, stub declarations are rarely needed in Perl 6 because post-declaration of functions is allowed, and normal [http://design.perl6.org/S06.html#Subroutines_and_other_code_objects function declarations] do not bend the syntax the way they sometimes do in Perl 5.
Use explicit forward definitions. Optional, unless (for instance) you want to use named parameters in a forward call.<br>
Should be identical to the actual definition, but preceded by "forward" and with no body.
<!--<syntaxhighlight lang="phix">-->
<span style="color: #008080;">forward</span> <span style="color: #008080;">function</span> <span style="color: #000000;">noargs</span><span style="color: #0000FF;">()</span> <span style="color: #000080;font-style:italic;">-- Declare a function with no arguments</span>
<span style="color: #008080;">forward</span> <span style="color: #008080;">procedure</span> <span style="color: #000000;">twoargs</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- Declare a procedure with two arguments</span>
<span style="color: #008080;">forward</span> <span style="color: #008080;">procedure</span> <span style="color: #000000;">twoargs</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000080;font-style:italic;">/*b*/</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- Parameter names are optional in forward (and actual) definitions</span>
<span style="color: #008080;">forward</span> <span style="color: #008080;">function</span> <span style="color: #000000;">anyargs</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- varargs are [best/often] handled as a (single) sequence in Phix</span>
<span style="color: #008080;">forward</span> <span style="color: #008080;">function</span> <span style="color: #000000;">atleastonearg</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">...);</span> <span style="color: #000080;font-style:italic;">-- Default makes args optional (== actual defn)</span>
<!--</syntaxhighlight>-->
No special syntax is needed on actual or forward function definitions for named parameters, and calls are identical whether still forward or now fully defined.<br>
Defaults on optional parameters in forward definitions can also be dummy (but type compatible) values should the actual not yet be defined.
 
=={{header|PL/I}}==
Note that the <tt>...</tt> in all of these stub bodies is literally part of the declaration syntax.
<syntaxhighlight lang="pli">
declare s1 entry;
declare s2 entry (fixed);
declare s3 entry (fixed, float);
 
declare f1 entry returns (fixed);
A prototype declaration for a function that does not require arguments (and returns an Int):
declare f2 entry (float) returns (float);
<lang perl6>sub foo ( --> Int) {...}</lang>
declare f3 entry (character(*), character(*)) returns (character (20));
</syntaxhighlight>
 
=={{header|PureBasic}}==
A prototype declaration for a function that requires two arguments.
PureBasic defines both functions and procedures by using the keyword '''Procedure'''. For the purposes of this task I will describe both 'procedures' and 'functions' using only the term 'procedure'. PureBasic uses a one-pass compiler. All procedures need to be defined before use. The prototypes referred to in the task description are performed with forward declarations in PureBasic.
Note that we can omit the variable name and just use the sigil in the stub, since we don't need to reference the argument until the actual definition of the routine. Also, unlike in Perl 5, a sigil like <tt>@</tt> defaults to binding a single positional argument.
<lang perl6>sub foo (@, $ --> Int) {...}</lang>
 
PureBasic allows two types of prototyping.
A prototype declaration for a function that utilizes varargs after one required argument.
The first uses the keyword '''Declare''' and describes the name, return value, and parameters of a procedure.
Note the "slurpy" star turns the <tt>@</tt> sigil into a parameter that accepts all the rest of the positional arguments.
It is identical in form with the first line of a procedure definition with the exception that the keyword
<lang perl6>sub foo ($, *@ --> Int) {...}</lang>
'''Declare''' is used instead of the keyword 'Procedure'''.
It must be placed before the first use of the procedure and must occur before the procedure's definition.
The procedure declaration's parameteres need to match the order, type, and number of those in the procedure's
definition, though their names may be different.
 
The keyword '''ProtoType''' may be used for pointers to procedures so that a definition of the parameters and return type for the function being pointed to are defined and that the pointer may be used to execute the function with type checking. The parameter names do not have to match in the 'ProtoType' definition but the order, type and optional parameters do. 'ProtoTypes' must be defined before their first use.
A prototype declaration for a function that utilizes optional arguments after one required argument. Optionality is conferred by either a question mark or a default:
<lang perl6>sub foo ($, $?, $ = 42 --> Int) {...}</lang>
 
APureBasic prototypedoes declarationnot forallow aeither functionvariable thatarguments utilizesor named parameters:.
<syntaxhighlight lang="purebasic">;Forward procedure declare defined with no arguments and that returns a string
<lang perl6>sub foo ($, :$faster, :$cheaper --> Int) {...}</lang>
Declare.s booTwo()
;Forward procedure declare defined with two arguments and that returns a float
Declare.f moo(x.f, y.f)
;Forward procedure declare with two arguments and an optional argument and that returns a float
Declare.f cmoo(x.f, y.f, m.f = 0)
 
;*** The following three procedures are defined before their first use.
Example of prototype declarations for subroutines or procedures, which in Perl 6 is done simply by noting that nothing is returned:
;Procedure defined with no arguments and that returns a string
<lang perl6>sub foo ($, $ --> Nil) {...}</lang>
Procedure.s boo(): ProcedureReturn "boo": EndProcedure
;Procedure defined with two arguments and that returns an float
Procedure.f aoo(x.f, y.f): ProcedureReturn x + y: EndProcedure
;Procedure defined with two arguments and an optional argument and that returns a float
Procedure.f caoo(x.f, y.f, m.f = 1): ProcedureReturn (x + y) * m: EndProcedure
 
;ProtoType defined for any function with no arguments and that returns a string
A routine may also slurp up all the named arguments that were not bound earlier in the signature:
Prototype.s showString()
<lang perl6>sub foo ($, :$option, *% --> Int) {...}</lang>
;Prototype defined for any function with two float arguments and that returns a float
Prototype.f doMath(x.f, y.f)
;ProtoType defined for any function with two float arguments and an optional float argument and that returns a float
Prototype.f doMathWithOpt(x.f, y.f, m.f = 0)
 
Define a.f = 12, b.f = 5, c.f = 9
A routine may make a named parameter mandatory using exclamation mark. (This is more useful in multi subs than in stubs though.)
Define proc_1.showString, proc_2.doMath, proc_3.doMathWithOpt ;using defined ProtoTypes
<lang perl6>sub foo ($, :$option! --> Int) {...}</lang>
If OpenConsole("ProtoTypes and Forward Declarations")
PrintN("Forward Declared procedures:")
PrintN(boo())
PrintN(StrF(a, 2) + " * " + StrF(b, 2) + " = " + StrF(moo(a, b), 2))
PrintN(StrF(a, 2) + " * " + StrF(b, 2) + " + " + StrF(c, 2) + " = " + StrF(cmoo(a, b, c), 2))
PrintN(StrF(a, 2) + " * " + StrF(b, 2) + " = " + StrF(cmoo(a, b), 2))
;set pointers to second set of functions
proc_1 = @boo()
proc_2 = @aoo()
proc_3 = @caoo()
PrintN("ProtoTyped procedures (set 1):")
PrintN(proc_1())
PrintN(StrF(a, 2) + " ? " + StrF(b, 2) + " = " + StrF(proc_2(a, b), 2))
PrintN(StrF(a, 2) + " ? " + StrF(b, 2) + " ? " + StrF(c, 2) + " = " + StrF(proc_3(a, b, c), 2))
PrintN(StrF(a, 2) + " ? " + StrF(b, 2) + " = " + StrF(proc_3(a, b), 2))
;set pointers to second set of functions
proc_1 = @booTwo()
proc_2 = @moo()
proc_3 = @cmoo()
PrintN("ProtoTyped procedures (set 2):")
PrintN(proc_1())
PrintN(StrF(a, 2) + " ? " + StrF(b, 2) + " = " + StrF(proc_2(a, b), 2))
PrintN(StrF(a, 2) + " ? " + StrF(b, 2) + " ? " + StrF(c, 2) + " = " + StrF(proc_3(a, b, c), 2))
PrintN(StrF(a, 2) + " ? " + StrF(b, 2) + " = " + StrF(proc_3(a, b), 2))
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf
 
A routine may unpack an <tt>Array</tt> automaticly. Here the first element is stored in a scalar and the rest in an <tt>Array</tt>. Other buildin types can be [http://design.perl6.org/S06.html#Unpacking_array_parameters unpacked] as well.
<lang perl6>sub foo ([$, @]) {...}</lang>
 
;*** If the forward Declaration above are not used then the following Procedure
A routine may ask for a closure parameter to implement higher order functions. Typed or untyped signatures can be supplied.
;definitions each have to be placed before the call to the respective procedure.
<lang perl6>sub foo (@, &:(Str --> Int)) {...}</lang>
 
;Procedure defined with no arguments and that returns a string
=={{header|Phix}}==
Procedure.s booTwo()
As explicit forward definitions, and optional - unless (eg) you want to use named parameters in a forward call.<br>
ProcedureReturn "booTwo"
Should be identical to the actual definition, but preceded by "forward" and with no body.
EndProcedure
<lang Phix>forward function noargs() -- Declare a function with no arguments
forward procedure twoargs(integer a, integer b) -- Declare a procedure with two arguments
forward procedure twoargs(integer, integer /*b*/) -- Parameter names are optional in forward (and actual) definitions
forward function anyargs(sequence s) -- varargs are [best/often] handled as a (single) sequence in Phix
forward function atleastonearg(integer a, integer b=1, ...); -- Default makes args optional (== actual defn)</lang>
No special syntax is needed on actual or forward function definitions for named parameters, and calls are identical whether still forward or now fully defined.<br>
Defaults on optional parameters in forward definitions can also be dummy (but type compatible) values should the actual not yet be defined.
 
;Procedure defined with two arguments and that returns an float
=={{header|PL/I}}==
Procedure.f moo(x.f, y.f)
<lang pli>
ProcedureReturn x * y
declare s1 entry;
EndProcedure
declare s2 entry (fixed);
declare s3 entry (fixed, float);
 
;Procedure defined with two arguments and an optional argument and that returns an float
declare f1 entry returns (fixed);
Procedure.f cmoo(x.f, y.f, m.f = 0)
declare f2 entry (float) returns (float);
ProcedureReturn (x * y) + m
declare f3 entry (character(*), character(*)) returns (character (20));
EndProcedure
</lang>
</syntaxhighlight>
Sample output:
<pre>Forward Declared procedures:
boo
12.00 * 5.00 = 60.00
12.00 * 5.00 + 9.00 = 69.00
12.00 * 5.00 = 60.00
ProtoTyped procedures (set 1):
boo
12.00 ? 5.00 = 17.00
12.00 ? 5.00 ? 9.00 = 153.00
12.00 ? 5.00 = 0.00
ProtoTyped procedures (set 2):
booTwo
12.00 ? 5.00 = 60.00
12.00 ? 5.00 ? 9.00 = 69.00
12.00 ? 5.00 = 60.00</pre>
 
=={{header|Quackery}}==
 
In Quackery a "word" corresponds to a function or subroutine. If you want to make a forward declaration (typically but not exclusively for recursive or mutually recursive words) you would use <code>forward is</code>, and resolve the forward declaration with <code>resolves</code>.
 
For example, the naive recursive Fibonacci function. (Note that the texts in parentheses are stack comments and can be omitted. Their inclusion is good practice. No declaration of parameters or arguments is required in Quackery.)
 
<syntaxhighlight lang="text"> forward is fibonacci ( n --> n )
 
[ dup 2 < if done
dup 1 - fibonacci
swap 2 - fibonacci + ] resolves fibonacci ( n --> n )</syntaxhighlight>
 
=={{header|Racket}}==
Most of the points are covered in this program
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 1,022 ⟶ 1,618:
(define (varargs2 a . args) (void)) ;one obligatory argument and the rest are contained in the list
 
(define (optional-arg (a 5)) (void)) ;a defaults to 5</langsyntaxhighlight>
 
<tt>(void)</tt> is a function that returns "nothing", so this are prototypes that do nothing.
Although standard Racket doesn't allow type declarations, it allows contracts, so we can add this to the previous declarations
<langsyntaxhighlight lang="racket">
(provide (contract-out
[two-args (integer? integer? . -> . any)]))</langsyntaxhighlight>
then any module that imports the function can only pass integers to two-args.
 
Another way is using the <tt>typed/racket</tt> language, like this
<langsyntaxhighlight lang="racket">
#lang typed/racket
 
(: two-args (Integer Integer -> Any))
(define (two-args a b) (void))</langsyntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
There is no restriction on placement of prototype declarations. (Actually, we call them "stub declarations".) In fact, stub declarations are rarely needed in Raku because post-declaration of functions is allowed, and normal [http://design.raku.org/S06.html#Subroutines_and_other_code_objects function declarations] do not bend the syntax the way they sometimes do in Perl 5.
 
Note that the <tt>...</tt> in all of these stub bodies is literally part of the declaration syntax.
 
A prototype declaration for a function that does not require arguments (and returns an Int):
<syntaxhighlight lang="raku" line>sub foo ( --> Int) {...}</syntaxhighlight>
 
A prototype declaration for a function that requires two arguments.
Note that we can omit the variable name and just use the sigil in the stub, since we don't need to reference the argument until the actual definition of the routine. Also, unlike in Perl 5, a sigil like <tt>@</tt> defaults to binding a single positional argument.
<syntaxhighlight lang="raku" line>sub foo (@, $ --> Int) {...}</syntaxhighlight>
 
A prototype declaration for a function that utilizes varargs after one required argument.
Note the "slurpy" star turns the <tt>@</tt> sigil into a parameter that accepts all the rest of the positional arguments.
<syntaxhighlight lang="raku" line>sub foo ($, *@ --> Int) {...}</syntaxhighlight>
 
A prototype declaration for a function that utilizes optional arguments after one required argument. Optionality is conferred by either a question mark or a default:
<syntaxhighlight lang="raku" line>sub foo ($, $?, $ = 42 --> Int) {...}</syntaxhighlight>
 
A prototype declaration for a function that utilizes named parameters:
<syntaxhighlight lang="raku" line>sub foo ($, :$faster, :$cheaper --> Int) {...}</syntaxhighlight>
 
Example of prototype declarations for subroutines or procedures, which in Raku is done simply by noting that nothing is returned:
<syntaxhighlight lang="raku" line>sub foo ($, $ --> Nil) {...}</syntaxhighlight>
 
A routine may also slurp up all the named arguments that were not bound earlier in the signature:
<syntaxhighlight lang="raku" line>sub foo ($, :$option, *% --> Int) {...}</syntaxhighlight>
 
A routine may make a named parameter mandatory using exclamation mark. (This is more useful in multi subs than in stubs though.)
<syntaxhighlight lang="raku" line>sub foo ($, :$option! --> Int) {...}</syntaxhighlight>
 
A routine may unpack an <tt>Array</tt> automaticly. Here the first element is stored in a scalar and the rest in an <tt>Array</tt>. Other buildin types can be [http://design.raku.org/S06.html#Unpacking_array_parameters unpacked] as well.
<syntaxhighlight lang="raku" line>sub foo ([$, @]) {...}</syntaxhighlight>
 
A routine may ask for a closure parameter to implement higher order functions. Typed or untyped signatures can be supplied.
<syntaxhighlight lang="raku" line>sub foo (@, &:(Str --> Int)) {...}</syntaxhighlight>
 
=={{header|REXX}}==
Line 1,068 ⟶ 1,702:
To begin with, we look at the definition provided [http://rosettacode.org/wiki/Function_definition#SNOBOL4 at the relevant task page]:
 
<langsyntaxhighlight lang="snobol4"> define('multiply(a,b)') :(mul_end)
multiply multiply = a * b :(return)
mul_end
Line 1,075 ⟶ 1,709:
output = multiply(10.1,12.2)
output = multiply(10,12)
end</langsyntaxhighlight>
 
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>.
Line 1,085 ⟶ 1,719:
Of course this implies that you can separate the two pieces. Which you can, like this:
 
<langsyntaxhighlight lang="snobol4"> define('multiply(a,b)')
 
*
Line 1,105 ⟶ 1,739:
output = multiply(10.1,12.2)
output = multiply(10,12)
end</langsyntaxhighlight>
 
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.
Line 1,120 ⟶ 1,754:
Thus a highly-contrived example function that illustrates all of these would look like this:
 
<langsyntaxhighlight lang="snobol4"> define('multiply(a,b)acc1,acc2','mult_impl') :(mult_end)
mult_impl acc1 = a
acc2 = b
Line 1,129 ⟶ 1,763:
output = multiply(10.1,12.2)
output = multiply(10,12)
end</langsyntaxhighlight>
 
=={{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.
<syntaxhighlight lang="wren">var factorial // forward declaration
 
factorial = Fn.new { |n| (n <= 1) ? 1 : factorial.call(n-1) * n }
 
System.print(factorial.call(5))</syntaxhighlight>
 
{{out}}
<pre>
120
</pre>
 
=={{header|zkl}}==
In zkl, all functions are var args. Prototypes provide provide some documentation and an overlay on the incoming args. Named parameters are not supported.
<langsyntaxhighlight lang="zkl">fcn{"Hello World"} // no expected args
fcn(){"Hello World"} // ditto
 
Line 1,156 ⟶ 1,809:
 
// no type enforcement but you can give a hint to the compiler
fcn([Int]n){n.sin()} //--> syntax error as Ints don't do sin</langsyntaxhighlight>
 
{{omit from|AutoHotkey}}
Line 1,179 ⟶ 1,832:
{{omit from|NetRexx}}
{{omit from|PicoLisp}}
{{omit from|Processing|No such thing as a function prototype.}}
{{omit from|Python}}
{{omit from|R}}
9,476

edits