Skip to main content
Engineering LibreTexts

5.2: Defining Functions

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

    A function is like a script, except that each function has its own workspace, so any variables defined inside a function only exist while the function is running and don’t interfere with variables in other workspaces, even if they have the same name. Function inputs and outputs are defined carefully to avoid unexpected interactions.

    To define a new function, you create an M-file with the name you want and put a function definition in it. For example, to create a function named myfunc, create an M-file named myfunc.m and put the following definition into it (Listing 5.1):

    List 5.1: A function definition

    function res = myfunc(x)
        s = sin(x)
        c = cos(x)
        res = abs(s) + abs(c)

    The first non-comment word of the file has to be function, because that’s how MATLAB tells the difference between a script and a function file.

    A function definition is a compound statement. The first line is called the signature of the function; it defines the inputs and outputs of the function. In Listing 5.1 the input variable is named x. When this function is called, the argument provided by the user will be assigned to x.

    The output variable is named res, which is short for result. You can call the output variable whatever you want, but as a convention, I like to call it res. Usually the last thing a function does is assign a value to the output .

    Once you’ve defined a new function, you call it the same way you call built-in MATLAB functions. If you call the function as a statement, MATLAB puts the result into ans:

    >> myfunc(1)
    s = 0.84147098480790
    c = 0.54030230586814
    res = 1.38177329067604
    ans = 1.38177329067604

    But it’s more common (and better style) to assign the result to a variable:

    >> y = myfunc(1)
    s = 0.84147098480790
    c = 0.54030230586814
    res = 1.38177329067604
    y = 1.38177329067604

    While you’re debugging a new function, you might want to display intermediate results like this, but once it’s working, you’ll want to add semicolons to make it a silent function. A silent function computes a result but doesn’t display anything (except sometimes warning messages). Most built-in functions are silent.

    Each function has its own workspace, which is created when the function starts and destroyed when the function ends. If you try to access (read or write) the variables defined inside a function, you will find that they don’t exist.

    >> clear
    >> y = myfunc(1);
    >> who
    Your variables are: y
    >> s
    Undefined function or variable 's'.

    The only value from the function that you can access is the result, which in this case is assigned to y.

    If you have variables named s or c in your workspace before you call myfunc, they will still be there when the function completes.

    >> s = 1;
    >> c = 1;
    >> y = myfunc(1);
    >> s, c
    s = 1
    c = 1

    So inside a function you can use whatever variable names you want without worrying about collisions.

    This page titled 5.2: Defining Functions is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.