Function prototype: Difference between revisions

From Rosetta Code
Content added Content deleted
(PARI)
Line 65: Line 65:


<lang c>long
<lang c>long
foo(GEN a, GEN b)</code>
foo(GEN a, GEN b)</lang>
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:
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:
<lang c>/*
<lang c>/*

Revision as of 15:35, 26 November 2011

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

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.