Function prototype: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Diego}}: minor adjustments)
m (syntax highlighting fixup automation)
Line 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 59:
end;
 
end Stack;</langsyntaxhighlight>
 
To use the package and function:
<langsyntaxhighlight Adalang="ada">with Stack;
procedure Main is
N:integer:=5;
Line 69:
...
N := Pull;
end Main;</langsyntaxhighlight>
 
=={{header|Aime}}==
<langsyntaxhighlight lang="aime">integer f0(void); # No arguments
void f1(integer, real); # Two arguments
real f2(...); # Varargs
Line 81:
# 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</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
Line 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 123:
END COMMENT
 
SKIP</langsyntaxhighlight>
 
=={{header|Amazing Hopper}}==
Line 129:
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.
 
<syntaxhighlight lang="amazing hopper">
<lang Amazing Hopper>
#!/usr/bin/hopper
 
Line 166:
 
{0}return
</syntaxhighlight>
</lang>
 
=={{header|C}}==
Line 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 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 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 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 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 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 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 303:
01 ret PIC S9(9) USAGE COMP-5.
PROCEDURE DIVISION USING arg RETURNING ret.
END PROGRAM foreign-func.</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
Line 310:
Caveat -- This works with specific implementations of CL. This was tested in SBCL.
 
<langsyntaxhighlight lang="lisp">
(declaim (inline no-args))
(declaim (inline one-arg))
Line 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 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]
<langsyntaxhighlight lang="d">/// Declare a function with no arguments that returns an integer.
int noArgs();
 
Line 393:
}
 
void main() {}</langsyntaxhighlight>
 
=={{header|Delphi}}==
Line 401:
See [http://docwiki.embarcadero.com/RADStudio/Sydney/en/Class_and_Record_Helpers_(Delphi) Documentation] for more details.<br>
 
<syntaxhighlight lang="delphi">
<lang Delphi>
program Function_prototype;
 
Line 522:
Readln;
 
end.</langsyntaxhighlight>
{{out}}
<pre> Array: [-1, 1, 2, 3, ]
Line 534:
{{libheader| System.SysUtils}}
{{libheader| System.Classes}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Function_prototype_class;
 
Line 587:
end;
readln;
end.</langsyntaxhighlight>
 
=={{header|Diego}}==
<langsyntaxhighlight 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
Line 675:
for_var(v)_until[numArgsOut]_calc([sum]+=[v]);
[voo]_ret([sum]/[numArgsOut]);
end_funct[];</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):
<langsyntaxhighlight lang="fsharp">// A function taking and returning nothing (unit).
val noArgs : unit -> unit
// A function taking two integers, and returning an integer.
Line 701:
//optional arguments must be tupled.
member OptionalArgs : ?x:int * ?y:int -> int
end</langsyntaxhighlight>
 
=={{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 740:
Private:
i As Integer
End Type</langsyntaxhighlight>
 
=={{header|Go}}==
Line 746:
 
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.
 
Line 758:
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 767:
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 798:
=={{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 857:
0 2 4 6 8
0 3 6 9 12
0 4 8 12 16</langsyntaxhighlight>
 
=={{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 900:
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 942:
l.pop(); // 15
l.length; // 2
</syntaxhighlight>
</lang>
 
=={{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:
 
<langsyntaxhighlight lang="julia">julia > function mycompare(a, b)::Cint
(a < b) ? -1 : ((a > b) ? +1 : 0)
end
mycompare (generic function with 1 method)
</syntaxhighlight>
</lang>
 
Using @cfunction to create a prototype for passing this to C's quicksort:
 
<langsyntaxhighlight lang="julia">julia> mycompare_c = @cfunction(mycompare, Cint, (Ref{Cdouble}, Ref{Cdouble}))
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
Line 964:
 
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 998:
println(d.roo())
println(d.poo)
}</langsyntaxhighlight>
 
{{out}}
Line 1,009:
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">function Func() -- Does not require arguments
return 1
end
Line 1,023:
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 1,033:
 
function anyargs(xs: ...): int = ? ;;
function plusargs(x:int, xs: ...): int = ? ;;</langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
Line 1,041:
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 1,055:
}
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 1,104:
}
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 1,160:
}
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 1,215:
}
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 1,235:
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}}==
 
<langsyntaxhighlight lang="ocaml">(* Usually prototype declarations are put in an interface file,
a file with .mli filename extension *)
 
Line 1,262:
 
(* A prototype declaration for a function with polymorphic argument *)
val poly_arg : 'a -> unit</langsyntaxhighlight>
 
=={{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 1,285:
 
=={{header|OxygenBasic}}==
<syntaxhighlight lang="text">
'DECLARE FUNCTION' ABBREVIATED TO '!'
 
Line 1,324:
end extern ' revert to internal function mode
 
</syntaxhighlight>
</lang>
 
=={{header|PARI/GP}}==
Line 1,331:
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 1,356:
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|Phix}}==
Line 1,365:
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.
<!--<langsyntaxhighlight Phixlang="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>
Line 1,371:
<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>
<!--</langsyntaxhighlight>-->
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}}==
<langsyntaxhighlight lang="pli">
declare s1 entry;
declare s2 entry (fixed);
Line 1,384:
declare f2 entry (float) returns (float);
declare f3 entry (character(*), character(*)) returns (character (20));
</syntaxhighlight>
</lang>
 
=={{header|PureBasic}}==
Line 1,400:
 
PureBasic does not allow either variable arguments or named parameters.
<langsyntaxhighlight lang="purebasic">;Forward procedure declare defined with no arguments and that returns a string
Declare.s booTwo()
;Forward procedure declare defined with two arguments and that returns a float
Line 1,477:
ProcedureReturn (x * y) + m
EndProcedure
</syntaxhighlight>
</lang>
Sample output:
<pre>Forward Declared procedures:
Line 1,501:
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 )</langsyntaxhighlight>
 
=={{header|Racket}}==
Most of the points are covered in this program
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 1,520:
(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}}==
Line 1,543:
 
A prototype declaration for a function that does not require arguments (and returns an Int):
<syntaxhighlight lang="raku" perl6line>sub foo ( --> Int) {...}</langsyntaxhighlight>
 
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" perl6line>sub foo (@, $ --> Int) {...}</langsyntaxhighlight>
 
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" perl6line>sub foo ($, *@ --> Int) {...}</langsyntaxhighlight>
 
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" perl6line>sub foo ($, $?, $ = 42 --> Int) {...}</langsyntaxhighlight>
 
A prototype declaration for a function that utilizes named parameters:
<syntaxhighlight lang="raku" perl6line>sub foo ($, :$faster, :$cheaper --> Int) {...}</langsyntaxhighlight>
 
Example of prototype declarations for subroutines or procedures, which in Raku is done simply by noting that nothing is returned:
<syntaxhighlight lang="raku" perl6line>sub foo ($, $ --> Nil) {...}</langsyntaxhighlight>
 
A routine may also slurp up all the named arguments that were not bound earlier in the signature:
<syntaxhighlight lang="raku" perl6line>sub foo ($, :$option, *% --> Int) {...}</langsyntaxhighlight>
 
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" perl6line>sub foo ($, :$option! --> Int) {...}</langsyntaxhighlight>
 
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" perl6line>sub foo ([$, @]) {...}</langsyntaxhighlight>
 
A routine may ask for a closure parameter to implement higher order functions. Typed or untyped signatures can be supplied.
<syntaxhighlight lang="raku" perl6line>sub foo (@, &:(Str --> Int)) {...}</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 1,604:
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,611:
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,621:
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,641:
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,656:
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,665:
output = multiply(10.1,12.2)
output = multiply(10,12)
end</langsyntaxhighlight>
 
=={{header|Wren}}==
Line 1,675:
 
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.
<langsyntaxhighlight lang="ecmascript">var factorial // forward declaration
 
factorial = Fn.new { |n| (n <= 1) ? 1 : factorial.call(n-1) * n }
 
System.print(factorial.call(5))</langsyntaxhighlight>
 
{{out}}
Line 1,688:
=={{header|zkl}}==
In zkl, all functions are var args. Prototypes 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,711:
 
// 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}}
10,327

edits