prev | toc | next
 

3.2 Overview: MUF (cont'd)

Calling Functions:

So far, all versions of our program have only included one function, but programs can have as many functions as you want.

Functions (also called `words') are blocks of code executed as a single unit. They begin with a colon followed by the name of the function, and end with a semi-colon. The name of a function can be pretty much anything you want, but should include at least one non-numeric character. You should also avoid using function names already defined in MUF (type man <word> to see if the function name is already being used.)

Once they have been defined, functions can be called simply by including their name in the program's code. The following version of Tinker.muf does the same thing as the previous two, but this time the `greater' or `less than' notifications are handled by separate functions:

====================================
lvar ourNumber

: TellTrue

    me @ "Yes, the number is greater than one million." notify
;

: TellFalse

    me @ "No, the number is less than one million." notify
;

: main

    random ourNumber !

    ourNumber @ 1000000 > if
        TellTrue
    else
        TellFalse
    then

    me @ ourNumber @ intostr notify
;
====================================

prev | toc | top | next