Function prototype

From Rosetta Code
Revision as of 16:35, 18 March 2012 by rosettacode>Xenoker (Added Ada)
Function prototype is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Some languages provide the facility to declare functions and subroutines through the use of 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

Ada

  • Prototypes must be an exact copy of everything prior to the "is" statement for a function or procedure.
  • All prototypes must appear in a declarative section, ie: before a "begin" statement.
  • For a main program, prototypes are only necessary if a function call appears in the source before the function definition.
  • For a with'd file, prototypes must appear as part of the specification(.ads) file, and do not appear in the body file(.adb).

<lang 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);</lang> 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: <lang 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;</lang>

Common Lisp

The lambda list is part of the function definition. It describes the arguments to a function.

<lang lisp>;; 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))</lang>

When a program calls a function, Common Lisp uses the lambda list to check the number and type of arguments. Calls like (0args "extra arg"), (2args) and (2ints 3.0 4/5) would cause errors.

The lambda list has several more features.

<lang lisp>;; 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)</lang>

PARI/GP

GP does not use function prototypes.

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 GP; comment. For a function

<lang c>long foo(GEN a, GEN b)</lang> which takes two (required) t_INT arguments, returns a small integer (a C long) and appears as bar to the gp interpreter, the following command would be used: <lang c>/* GP;install("foo","LGG","bar","./filename.gp.so");

  • /</lang>

If its arguments were optional it could be coded as <lang c>/* GP;install("foo","LDGDG","bar","./filename.gp.so");

  • /</lang>

although other parser codes are possible; this one sends NULL if the arguments are omitted.

A code like <lang c>/* GP;install("foo","s*","bar","./filename.gp.so");

  • /</lang>

can be used to take a variable (0 or more) number of arguments. Note that the return type in this case is implicitly GEN

Other special forms are described in the User's Guide to the PARI library, section 5.7.3.