Skip to main content
Library homepage
 
Engineering LibreTexts

5.4.1: Function Names and MATLAB Keywords

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

    User defined MATLAB function names follow the same rules as variable names:

    The function name must start with a letter.

    It may have numbers and/or an underscore "_"

    It cannot have a space, a hyphen, parentheses, a period, nor another symbol.

    The function filename and the name on the function declaration line (a.k.a. signature) inside the file should be the same. See this link: https://www.mathworks.com/help/matlab/ref/function.html

    Do not give a user-defined function the same name as a built-in function. That causes a "name collision". You can check the name like this:

    which sum % MATLAB will tell you that this is a built-in function and will display the path to the library which contains this function.

    which my_super_function % MATLAB will tell you that this function is "not found", so it is OK to use as a function name.

    If you give your function the name of a built-in function, you can undo this by renaming your function, then enter this command:

    clear functions

    MATLAB has keywords that are reserved for its own use.
    You cannot use these words as names of variables or functions.
    Entering "iskeyword" gives a list of MATLAB's keywords in alphabetical order:

    'break'
    'case'
    'catch'
    'classdef'
    'continue'
    'else'
    'elseif'
    'end'
    'for'
    'function'
    'global'
    'if'
    'otherwise'
    'parfor'
    'persistent'
    'return'
    'spmd'
    'switch'
    'try'
    'while'

    This video gives a clear explanation of defining a MATLAB function:

    https://www.youtube.com/watch?v=qo3AtBoyBdM

    Function M-Files in MATLAB, RobertTalbertPhD


    This page titled 5.4.1: Function Names and MATLAB Keywords is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith.