Task

Demonstrate the different syntax and semantics provided for calling a function.



This may include:

   Calling a function that requires no arguments
   Calling a function with a fixed number of arguments
   Calling a function with optional arguments
   Calling a function with a variable number of arguments
   Calling a function with named arguments
   Using a function in statement context
   Using a function in first-class context within an expression
   Obtaining the return value of a function
   Distinguishing built-in functions and user-defined functions
   Distinguishing subroutines and functions
   Stating whether arguments are passed by value or by reference
   Is partial application possible and how


This task is not about defining functions.




a-function         \ requiring no arguments
a-function         \ with a fixed number of arguents
a-function         \ having optional arguments
a-function         \ having a variable number of arguments
a-function         \ having such named arguments as we have in Forth
' a-function var ! \ using a function in a first-class context (here: storing it in a variable)
a-function         \ in which we obtain a function's return value
 
                   \ forth lacks 'statement contenxt'
                   \ forth doesn't distinguish between built-in and user-defined functions
                   \ forth doesn't distinguish between functions and subroutines
                   \ forth doesn't care about by-value or by-reference
 
\ partial application is achieved by creating functions and manipulating stacks
: curried  0 a-function ;
: only-want-third-argument 1 2 rot a-function ;
 
\ Realistic example:
: move ( delta-x delta-y -- )
  y +!  x +! ;
 
: down ( n -- )  0 swap move ;
: up ( n -- )    negate down ;
: right ( n -- ) 0 move ;
: left ( n -- )  negate right ;