Program header and method declaration

A program needs the following:

At least the ``main'' method must be defined in your code. It will be called by the interpreter first to run your script. Your code might actually implement sub-methods as well. A method declaration looks like:

  1. start with the tag ``!'' (required only for sub-method declaration)
  2. give the name of the method (e.g. main)
  3. possibly have a declaration of local variables: ``|'', a list of their names, separated by spaces, and ``|'' again
  4. next are the objects and their messages as your programming code
  5. and last is the return statement: ^self (or another value)
To call a sub-method you just created in your program, pass its name and the parameters (if there are some) to the object ``self'' (the representation of your program).

Example:

[ |

main

   | myVariable |
   myVariable := 333.87 .
   self printValue: myVariable.     " call ourself with message printValue: and pass myVariable as parameter "

!
printValue: val     " declaration of method printValue: which receives a paramter in the local variable val "
   | localVar |
   localVar := val description.     " call the message description on val "
   Transcript showLine: ('value = ', localVar).
   ^self     " return our identification (could be any object) back to caller "

]

moltalk@moltalk.org      version of this document: V3.0