Function prototype: Difference between revisions

Added Racket code
(Added Racket code)
Line 225:
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>
 
=={{header|Racket}}==
Most of the points are covered in this program
<lang Racket>#lang racket
(define (no-arg) (void))
 
(define (two-args a b) (void)) ;arguments are always named
 
(define (varargs . args) (void)) ;the extra arguments are stored in a list
 
(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>
 
(void) 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>
then any module that imports the function can only pass integers to two-args.
 
Another way is using the typed/racket language, like this
<lang Racket>#lang typed/racket
 
(: two-args (Integer Integer -> Any))
(define (two-args a b) (void))</lang>
 
{{omit from|AutoHotkey}}
Anonymous user