Function prototype: Difference between revisions

Added COBOL examples.
m (Added Erlang)
(Added COBOL examples.)
Line 31:
next : accBox; -- including that a box holds access to other boxes
end record;</lang>
Example of a package specification (i.e. prototype).:
<lang Ada>package Stack is
package Stack is
procedure Push(Object:Integer);
function Pull return Integer;
end Stack;</lang>
</lang>
 
Example of a package body:
<lang Ada>package body Stack is
package body Stack is
 
procedure Push(Object:Integer) is
Line 53 ⟶ 50:
end;
 
end Stack;</lang>
</lang>
 
To use the package and function:
<lang Ada>with Stack;
with Stack;
procedure Main is
N:integer:=5;
Line 65 ⟶ 60:
...
N := Pull;
end Main;</lang>
</lang>
 
=={{header|Aime}}==
Line 89 ⟶ 83:
int anyargs(...); /* An ellipsis can be used to declare a function that accepts varargs */
int atleastoneargs(int, ...); /* One mandatory integer argument followed by varargs */</lang>
 
=={{header|COBOL}}==
 
Prototypes were introduced in COBOL 2002, and are typically used by <code>COPY</code>ing the copybook in which they are contained into the beginning of the source file. 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.
 
<lang cobol> *> A subprogram taking no arguments and returning nothing.
PROGRAM-ID. no-args PROTOTYPE.
END PROGRAM no-args.
 
*> A subprogram taking two 8-digit numbers as arguments (passed by
*> value), and returning an 8-digit number.
PROGRAM-ID. two-args PROTOTYPE.
DATA DIVISION.
LINKAGE SECTION.
01 arg-1 PIC 9(8).
01 arg-2 PIC 9(8).
01 ret PIC 9(8).
PROCEDURE DIVISION USING BY VALUE arg-1, arg-2 RETURNING ret.
END PROGRAM two-args.
 
*> A subprogram taking two optional arguments which are 8-digit
*> numbers (passed by reference (the default and compulsory for
*> optional arguments)).
PROGRAM-ID. optional-args PROTOTYPE.
DATA DIVISION.
LINKAGE SECTION.
01 arg-1 PIC 9(8).
01 arg-2 PIC 9(8).
PROCEDURE DIVISION USING OPTIONAL arg-1, OPTIONAL arg-2.
END PROGRAM optional-args.
 
*> Standard COBOL does not support varargs or named parameters.
 
*> A function from another language, taking a 32-bit integer by
*> value and returning a 32-bit integer (in Visual COBOL).
PROGRAM-ID. foreign-func PROTOTYPE.
OPTIONS.
ENTRY-CONVENTION some-langauge.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 arg PIC S9(9) USAGE COMP-5.
01 ret PIC S9(9) USAGE COMP-5.
PROCEDURE DIVISION USING arg RETURNING ret.
END PROGRAM foreign-func.</lang>
 
=={{header|Common Lisp}}==
Line 175 ⟶ 213:
=={{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.
<lang J> NB. j assumes an unknown name f is a verb of infinite rank
<lang J>
NB. j assumes an unknown name f is a verb of infinite rank
NB. f has infinite ranks
f b. 0
Line 235 ⟶ 272:
0 2 4 6 8
0 3 6 9 12
0 4 8 12 16</lang>
</lang>
 
=={{header|PARI/GP}}==
Line 269 ⟶ 305:
 
<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</lang>
</lang>
 
=={{header|Perl 6}}==
Line 305 ⟶ 340:
=={{header|Racket}}==
Most of the points are covered in this program
<lang racket>#lang racket
#lang racket
 
(define (no-arg) (void))
Line 316 ⟶ 350:
(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</lang>
</lang>
 
<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
<lang racket>(provide (contract-out
[two-args (integer? integer? . -> . any)]))</lang>
(provide (contract-out
[two-args (integer? integer? . -> . any)]))
</lang>
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
<lang racket>#lang typed/racket
#lang typed/racket
 
(: two-args (Integer Integer -> Any))
Anonymous user