**7.2** Function inputs and outputs

A mathematical function, like *y* = sin(*x*), can be thought of as a machine that takes a numerical value *x* as an input and returns a numerical value *y* as an output. The precise algorithm it employs to calculate the value is out-of-sight. MATLAB functions can be thought of in analogy with built-in mathematical functions like sine and cosine, but in fact, MATLAB functions operate more generally and play a much greater role. In computer programming, functions are the principal tools for managing complexity. They allow a complicated program to be broken up into many simpler parts, parts that are easier to understand, easier to write, and easier to test.

Functions can have inputs and outputs. Inputs are the values of variables that are sent to the function. There can be several inputs and they can be of any MATLAB class—real numbers, vectors, matrices, arrays, strings, and more. Functions can even take other functions as inputs (such a function is called a function function). Similarly, the output of the function can be any number of values of any class.

In addition to calculating the outputs based on the inputs, a function can have *side-effects*, that is, it can make something happen beyond assigning values to its outputs. We have ...

Start Free Trial

No credit card required