Current Page: NeXtMidas User's Guide Macros Creating a New Macro Macro Example
| #### Macro Example: triangle.mm The following example (`triangle.mm`) shows a more advanced macro that computes information about a triangle. (Again, the line numbers appear as a reference tool for illustration purposes; they are not part of the macro.)
     1: ! This computes some information about a right triangle.
     2: !
     3: ! Parameters:
     4: !   [angle]   The measurement of one angle in Deg.
     5: !   [lengthH] The length of the hypotenuse in mm.
     6: !
     7: startmacro d:angle l:lengthH
     8:  calc l:lengthA lengthH angle sind *
     9:  calc l:lengthB lengthH angle cosd *
    10:
    11:  res type "General Right Triangle"
    12:
    13:  if angle EQ 45 then
    14:    res type "45 Deg. Right Triangle"
    15:  endif
    16:
    17:  if angle EQ 60 OR angle EQ 30 then
    18:    res type "30-60 Right Triangle"
    19:  endif
    20:
    21:   say "The given measurements define a ^type"
    22:   say "with sides that are ^lengthA mm and ^lengthB mm long."
    23: endmacro
Although it is significantly longer than the simple`mymacro.mm` macro, it is not any more complex. Let's look at it line by line: Lines 1 - 6: All lines beginning with an exclamation-point (also called a "bang") are comments and ignored by NeXtMidas. By using comments, a programmer explains the macro's function. Line 7: The `startmacro` line has two parameters: `angle` and `lengthH`. It also specifies the type for `angle` as Midas double (`"d:"`) and `lengthH`.as Midas long (`"l:"`). If this macro is called with inappropriate values, NeXtMidas attempts to make the necessary conversions. However, if NeXtMidas can not make the conversions it displays an error message. Lines 8 - 9: These lines perform calculations using the values passed in. (See `HELP CALC` for details.) Line 11: This creates a results variable type. Although a macro can access the results set that exists in the environment in which it was called, it uses its own results set when setting results. Therefore, a macro will not accidentally overwrite results that belong to someone else and the results created by the macro are removed when the macro ends. To modify the results outside of the current macro, use the switches provided by the `RESULTS` and `RUN` commands (see `HELP RESULTS` and `HELP RUN` for details). Lines 13 - 15: Here is the first new command used inside a macro that is not used at the command line. The `IF` statement executes code when the given condition is true. In this case, if `angle` is equal (`EQ`) to 45 then line 14 is executed. Otherwise, NeXtMidas skips to the end of the `IF` statement (signified by `endif`) and resumes execution with the next line. Lines 17 - 19: This `IF` statement executes line 18 if `angle` is equal to 60 or if is`angle` equal to 30. For more details on how to perform different comparisons or how to use more complex compare `IF` statements see `HELP IF`. Lines 21 - 22: These lines print out information relevant to the user. Line 23: The macro ends with `endmacro`. To run this macro, type the lines into a text editor and save the file as `triangle.mm`. Then start NeXtMidas and try running the macro with each of the following angle and length values:
    nM> %mymacro 15 100
    nM> %mymacro 30 100
    nM> %mymacro 45 80
    nM> %mymacro 30 "abc"
After you complete each test, you should find that all of the given values work except the last one (`d: 30` and `l:abc`). Because `lenghH(abc`) is an inappropriate value, NeXtMidas diplays an error message.