This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,12 @@
Some languages provide the facility to declare functions and subroutines through the use of [[wp:Function prototype|function prototyping]]. The task is to demonstrate the methods available for declaring prototypes within the language. The provided solutions should include:
* An explanation of any placement restrictions for prototype declarations
* A prototype declaration for a function that does not require arguments
* A prototype declaration for a function that requires two arguments
* A prototype declaration for a function that utilizes varargs
* A prototype declaration for a function that utilizes optional arguments
* A prototype declaration for a function that utilizes named parameters
* Example of prototype declarations for subroutines or procedures (if these differ from functions)
* An explanation and example of any special forms of prototyping not covered by the above
Languages that do not provide function prototyping facilities should be omitted from this task.

View file

@ -0,0 +1,6 @@
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);

View file

@ -0,0 +1,5 @@
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;

View file

@ -0,0 +1,4 @@
package Stack is
procedure Push(Object:Integer);
function Pull return Integer;
end Stack;

View file

@ -0,0 +1,13 @@
package body Stack is
procedure Push(Object:Integer) is
begin
-- implementation goes here
end;
function Pull return Integer;
begin
-- implementation goes here
end;
end Stack;

View file

@ -0,0 +1,8 @@
with Stack;
procedure Main is
N:integer:=5;
begin
Push(N);
...
N := Pull;
end Main;

View file

@ -0,0 +1,5 @@
int noargs(); /* Declare a function with no arguments that returns an integer */
int twoargs(int a,int b); /* Declare a function with no arguments that returns an integer */
int twoargs(int ,int); /* Parameter names are optional in a prototype definition */
int anyargs(...); /* An ellipsis can be used to declare a function that accepts varargs */
int atleastoneargs(int, ...); /* One mandatory integer argument followed by varargs */

View file

@ -0,0 +1,13 @@
;; An empty lambda list () takes 0 parameters.
(defun 0args ()
(format t "Called 0args~%"))
;; This lambda list (a b) takes 2 parameters.
(defun 2args (a b)
(format t "Called 2args with ~A and ~A~%" a b))
;; Local variables from lambda lists may have declarations.
;; This function takes 2 arguments, which must be integers.
(defun 2ints (i j)
(declare (type integer i j))
(/ (+ i j) 2))

View file

@ -0,0 +1,8 @@
;; Rest parameter, for variadic functions: r is a list of arguments.
(a b &rest r)
;; Optional parameter: i defaults to 3, (f 1 2) is same as (f 1 2 3).
(a b &optional (i 3))
;; Keyword parameters: (f 1 2 :c 3 :d 4) is same as (f 1 2 :d 4 :c 3).
(a b &key c d)

View file

@ -0,0 +1,47 @@
/// Declare a function with no arguments that returns an integer.
int noArgs();
/// Declare a function with no arguments that returns an integer.
int twoArgs(int a, int b);
/// Parameter names are optional in a prototype definition.
int twoArgs2(int, int);
/// An ellipsis can be used to declare a function that accepts
/// C-style varargs.
int anyArgs(...);
/// One mandatory integer argument followed by C-style varargs.
int atLeastOneArg(int, ...);
/// Declare a function that accepts any number of integers.
void anyInts(int[] a...);
/// Declare a function that accepts D-style varargs.
void anyArgs2(TArgs...)(TArgs args);
/// Declare a function that accepts two or more D-style varargs.
void anyArgs3(TArgs...)(TArgs args) if (TArgs.length > 2);
/// Currently D doesn't support named arguments.
/// One implementation.
int twoArgs(int a, int b) {
return a + b;
}
interface SomeInterface {
void foo();
void foo(int, int);
// Varargs
void foo(...); // C-style.
void foo(int[] a...);
void bar(T...)(T args); // D-style.
// Optional arguments are only supported if a default is provided,
// the default arg(s) has/have to be at the end of the args list.
void foo(int a, int b = 10);
}
void main() {}

View file

@ -0,0 +1,2 @@
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