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 ...
Get Learning to Program with MATLAB: Building GUI Tools now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.