Function prototype: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Omitted languages: add Processing)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 67:
N := Pull;
end Main;</lang>
 
=={{header|Aime}}==
<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</lang>
 
=={{header|ALGOL 68}}==
Line 109 ⟶ 121:
 
SKIP</lang>
 
=={{header|Aime}}==
<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</lang>
 
=={{header|C}}==
Line 348:
 
void main() {}</lang>
 
=={{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|FreeBASIC}}==
Line 387 ⟶ 413:
i As Integer
End Type</lang>
 
=={{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 615:
l.length; // 2
</lang>
 
 
=={{header|Julia}}==
Line 707 ⟶ 706:
function anyargs(xs: ...): int = ? ;;
function plusargs(x:int, xs: ...): int = ? ;;</lang>
 
 
=={{header|M2000 Interpreter}}==
Line 890 ⟶ 888:
Check
</lang>
 
 
=={{header|Nim}}==
Line 993 ⟶ 990:
sub noargs :prototype(); # Using the :attribute syntax instead
sub twoargs :prototype($$);</lang>
 
=={{header|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 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.
 
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):
<lang perl6>sub foo ( --> Int) {...}</lang>
 
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.
<lang perl6>sub foo (@, $ --> Int) {...}</lang>
 
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.
<lang perl6>sub foo ($, *@ --> Int) {...}</lang>
 
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>
 
A prototype declaration for a function that utilizes named parameters:
<lang perl6>sub foo ($, :$faster, :$cheaper --> Int) {...}</lang>
 
Example of prototype declarations for subroutines or procedures, which in Perl 6 is done simply by noting that nothing is returned:
<lang perl6>sub foo ($, $ --> Nil) {...}</lang>
 
A routine may also slurp up all the named arguments that were not bound earlier in the signature:
<lang perl6>sub foo ($, :$option, *% --> Int) {...}</lang>
 
A routine may make a named parameter mandatory using exclamation mark. (This is more useful in multi subs than in stubs though.)
<lang perl6>sub foo ($, :$option! --> Int) {...}</lang>
 
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>
 
A routine may ask for a closure parameter to implement higher order functions. Typed or untyped signatures can be supplied.
<lang perl6>sub foo (@, &:(Str --> Int)) {...}</lang>
 
=={{header|Phix}}==
Line 1,052 ⟶ 1,012:
declare f3 entry (character(*), character(*)) returns (character (20));
</lang>
 
=={{header|PureBasic}}==
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.
Line 1,189 ⟶ 1,150:
(: two-args (Integer Integer -> Any))
(define (two-args a b) (void))</lang>
 
=={{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 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.
 
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):
<lang perl6>sub foo ( --> Int) {...}</lang>
 
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.
<lang perl6>sub foo (@, $ --> Int) {...}</lang>
 
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.
<lang perl6>sub foo ($, *@ --> Int) {...}</lang>
 
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>
 
A prototype declaration for a function that utilizes named parameters:
<lang perl6>sub foo ($, :$faster, :$cheaper --> Int) {...}</lang>
 
Example of prototype declarations for subroutines or procedures, which in Perl 6 is done simply by noting that nothing is returned:
<lang perl6>sub foo ($, $ --> Nil) {...}</lang>
 
A routine may also slurp up all the named arguments that were not bound earlier in the signature:
<lang perl6>sub foo ($, :$option, *% --> Int) {...}</lang>
 
A routine may make a named parameter mandatory using exclamation mark. (This is more useful in multi subs than in stubs though.)
<lang perl6>sub foo ($, :$option! --> Int) {...}</lang>
 
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>
 
A routine may ask for a closure parameter to implement higher order functions. Typed or untyped signatures can be supplied.
<lang perl6>sub foo (@, &:(Str --> Int)) {...}</lang>
 
=={{header|REXX}}==
10,327

edits