Skip to main content
Engineering LibreTexts

13.2: User-Defined Functions

  • Page ID
    14993
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Sometimes you will need to create your own function in Mathcad, perhaps for repeated use throughout a worksheet. The syntax looks very similar to the way a function might appear in a math textbook. The function name is followed immediately by the input variables (in parentheses and separated by commas), a definition equals sign (colon), and then the expression in terms of the input variables. When the function is executed, the input variables are replaced with constants, including units as desired, and followed by the evaluation equals sign. Consider the following example.

    Example 13.2.1

    Create a function for the volume of a cylinder.

    Solution

    The function, called Volume, is defined in terms of the variables radius and height (don’t forget to use the assignment equals, created with a colon :).

    Volume(radius,height):=π radius2 height

    The symbol for π can be created on the Greek toolbar, or with the keyboard shortcut Ctrl + Shift + p. Now, when we want to compute with the function,
    we substitute values for radius and height (including any unit) into the function

    Volume(2ft,3ft):= 1.068 × 103 L

    or

    Volume(500cm,2000cm):= 1.571 × 106 L

    Note that Mathcad converts to the metric unit L (liters) as default.


    This page titled 13.2: User-Defined Functions is shared under a CC BY-NC 3.0 license and was authored, remixed, and/or curated by Troy Siemers (APEX Calculus) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.