Function prototype: Difference between revisions

Content added Content deleted
(Added zkl)
Line 511: Line 511:
(: two-args (Integer Integer -> Any))
(: two-args (Integer Integer -> Any))
(define (two-args a b) (void))</lang>
(define (two-args a b) (void))</lang>

=={{header|zkl}}==
In zkl, all functions are var args. Prototypes provide provide some documentation and an overlay on the incoming args. Named parameters are not supported.
<lang zkl>fcn{"Hello World"} // no expected args
fcn(){"Hello World"} // ditto

fcn{vm.arglist}(1,2) // ask the VM for the passed in args -->L(1,2)
fcn f(a,b){a+b} // fcn(1,2,3) works just fine
fcn f(args){}(1,2,3) //args = 1
fcn(args){vm.arglist.sum()}(1,2,3) //-->6

fcn(a=1,b=2){vm.arglist}() //-->L(1,2)
fcn(a=1,b=2){vm.arglist}(5) //-->L(5,2)
fcn(a=1,b){vm.arglist}() //-->L(1), error if you try to use b
fcn(a,b=2){vm.arglist}(5) //-->L(5,2)
fcn(a,b=2,c){vm.arglist}(1) //-->L(1,2)

fcn(){vm.nthArg(1)}(5,6) //-->6
fcn{vm.numArgs}(1,2,3,4,5,6,7,8,9) //-->9
fcn{vm.argsMatch(...)} // a somewhat feeble attempt arg pattern matching based on type (vs value)

// you can do list assignment in the prototype:
fcn(a,[(b,c)],d){vm.arglist}(1,L(2,3,4),5) //-->L(1,L(2,3,4),5)
fcn(a,[(b,c)],d){"%s,%s,%s,%s".fmt(a,b,c,d)}(1,L(2,3,4),5) //-->1,2,3,5

// no type enforcement but you can give a hint to the compiler
fcn([Int]n){n.sin()} //--> syntax error as Ints don't do sin</lang>


{{omit from|AutoHotkey}}
{{omit from|AutoHotkey}}