5: User-Defined Functions
- Page ID
- 84325
\( \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}}\)
- 5.1: Name Collisions
- Scripts may change external variables. Functions use local variables; they can’t change external variables.
- 5.2: Defining Functions
- A function's 1st line is it's signature.
- 5.3: Function Documentation
- You should document what the function does.
- 5.4: Naming Functions
- Function names follow the same rules as variable names. Use descriptive function names.
- 5.6: Functions with Multiple Outputs
- How to create and use MATLAB/Octave functions with multiple inputs
- 5.7: Matlab Functions with No Inputs
- Functions can have outputs, even though they have no input arguments.
- 5.8: Matlab Functions with No Outputs
- How to create and use MATLAB/Octave functions with no outputs. An example of drawing a star is given. Exercises of drawing a rectangle and a triangle are given.
- 5.9: Matlab Functions with No Inputs and No Outputs
- Some functions have no inputs and no returned variables, although they may display values, change the screen, or change a plot.
- 5.10: Functions with variable numbers of inputs or outputs
- How functions with variable numbers of inputs and/or outputs work
Most of the material in this chapter comes from Downey, chapter 5 and Lambert, chapter 8.