Search code examples
forthgforth

Forth function like structures


I am playing around with Forth, and I am trying to see if it has a function-like structure. I am trying to understand it coming from an OOP background.

I have seen you can have something like:

: addition + . ;
4 2 addition 6 ok

Is there a way to have it so you could have variables like:

: addition ( a b )
a b + ;
4 2 addition . 6 ok

So that I could do more complex things with that word definition.


Solution

  • The conventional syntax for local variables uses { and }. Your examples would become:

    : addition { a b -- } a b + ;
    

    The standard uses {: and :} instead.

    http://forth-standard.org/standard/locals/bColon