Skip to main content
Engineering LibreTexts

15.5: Using Functions and Subroutines

  • Page ID
    54361
  • \( \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}}\)

    Before a function or subroutine can be used, it must be defined or written. Once defined, the function or subroutine can be used or called. A function or subroutine is called by using its name as we have done with the intrinsic functions. When a program uses a subroutine, it is called with a call statement. When a program uses a function, it is used by name and returns a result which must be assigned somewhere appropriate (e.g., like a variable).

    Argument Passing

    When using functions or subroutines, information (values, variables, etc.) is typically passed to or from the routines. Argument association is a way of passing values from actual arguments to formal arguments. If an actual argument is an expression, it is evaluated and passed to the corresponding formal argument. If an actual argument is a variable or constant, its value is passed to the corresponding formal argument. There must be a one-to-one correspondence between the actual argument (calling routine) and the formal argument (function/subroutine).

    The arguments in the call are matched up to the arguments in the function/subroutine by position. Each of the arguments is matched by its corresponding position. The names of the variables do not need to match, however, the data types must match. For example, given the following subroutine call and subroutine,

    A calling function and subroutine.

    each variable is matched by its corresponding position.

    Other variables in either the calling routine (top) or the subroutine (bottom) are isolated from each other. As such, the same variable name can be re-used in both the calling routine and the subroutine (and refer to different values).


    This page titled 15.5: Using Functions and Subroutines is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?