Skip to main content
Engineering LibreTexts

6.4: Syntax - Inputs (Parameters) and Outputs

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

    Different languages require different syntax for the definition and use (call) of a function. We first consider the former and then the latter. By way of example, we present below the function \(\mathrm{x}_{2}\) to_the_ \(2 \mathrm{p}\) which given \(x\) evaluates the function (in this case, literally a mathematical function) \(x^{2 p}\).

    function [ value ] = x_to_the_2p( x, p ) 
    value = x.^(2*p); 
    end
    

    The first line declares that this script is a function, that the output will be returned in a variable value, that the function name - and also the name of the \(m\) file in which the function is stored - is x_to_the_2p, and that the function takes two arguments, \(x\) and \(p\). Next follows the body of the function which produces the output, value. The function closes with an end statement.

    We note that our little function takes a single-index (or even multi-index) array as input. In general, as described above, function calls can be expensive, and hence it is best to generate as much data as possible with each call so as to minimize the number of calls required. For that reason, it is often advantageous to define functions such that the (appropriate) inputs and outputs are arrays rather than scalars. (In our example above, value will be of the same size as \(x\). This is realized automatically through the assignment statement.) Of course scalars will be a special case of arrays and hence the function may still be called with scalar arguments.

    More generally the syntax for a function with \(\mathrm{J}\) inputs and K outputs is

    function [output 1, output 2, . . . , output K] = function name(input 1, input 2, ..., input J ) 
    BODY of FUNCTION 
    end
    

    Note that we may have not only multiple inputs but also multiple outputs. All outputs must be defined within the BODY of FUNCTION or MATLAB will complain.

    The operation of the function is fairly clear. First our little example (which we call here from the command window, but of course could also be called from a "main" program or another function program):

    >> clear all 
    >> y = x_to_the_2p( [1,2], 2) 
    y = 
        1 16 
    >> value ??? Undefined function or variable 'value'.
    >>
    

    Note in the above our function is evaluated and the output assigned to the variable y. The variable value is internal to the function and the calling program - in this case the function is called from the command-line mode and hence the calling program variables are simply the workspace - has no knowledge of this "dummy" variable.

    More generally, we call a function with \(\mathrm{J}\) outputs and \(\mathrm{K}\) inputs as [output_1, output_2 \(2 . \ldots\), output_ \(J]=\) function_name(input_ 1 , input_ \(2, \ldots\), input_ \(J\) ); . (Note that if we omit the semicolon then our outputs will all be displayed in the command window.) Upon being called by the calling program, function_name executes BODY of FUNCTION for the values of the input arguments passed to function_name and then upon reaching the end statement function_name returns the outputs - and control - to the calling program. Note is possible to force an early return of a function (to the calling program) before the end statement is encountered with a return statement within the BODY of FUNCTION.

    It is possible to request only the first \(K^{\prime}\) outputs as [output_ 1 , output_ \(2, \ldots\), output_ \(K^{\prime}\) ] = function_name (input_1, input_2, ..., input_ \(J\) ) ; . Particularly useful is the case in which you only require the first output, as in this case you can directly use the function through composition within a larger expression with the intermediary of assignment. Up to this point, with the exception of min, we have considered only single-output functions (or in any event only asked for the first output) in our examples - and for precisely this composition reason. Another example here:

    >> z = x_to_the_2p( [1,2], 2) + [2,3] 
    z = 
        3 19 
    >>
    

    Note it is important to distinguish between multiple outputs and arrays. An array corresponds to a particular output, not multiple outputs; in our example above, there is a single output, which happens to be a single-index array of length \(2 .\)

    It is also possible to call a function without all inputs specified, either with [] (null) entries or simply with a truncated list - the first \(J^{\prime}\) inputs. However, in this case, it is important that within the function all inputs that will be encountered are defined. In this regard, the MATLAB function isempty is useful (for nulls) and the MATLAB nargin is useful (for truncated argument lists) in order to detect any "unset" inputs which must be assigned to default values. (Note different programming languages have different mechanisms for dealing with defaults.) There is also an nargout MATLAB function useful in tailoring the outputs provided.


    This page titled 6.4: Syntax - Inputs (Parameters) and Outputs is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Masayuki Yano, James Douglass Penn, George Konidaris, & Anthony T Patera (MIT OpenCourseWare) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.