3.11: Arrays as Function Arguments

Fundamentals of MATLAB Programming · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From LibreTexts, under CC BY-NC-SA, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    3.11: Arrays as Function Arguments

    Many MATLAB built-in functions accept either a single number or an array. When given an array, the function often applies the operation to every element. For example, the abs function is used to find the absolute value of a single number or an array of numbers.

    Example \(\PageIndex{1}\)

    Using abs function.

    value_1 = abs(-5)
    
    
    value_2 = abs([3 -1 6 -10 3 -9])
    Solution
    value_1 =
    
    5
    
    
    value_2 =
    
    3 1 6 10 3 9

    The same idea can be used in your own functions. If your function uses array operations correctly, it can work for one value or many values. This is possible because MATLAB treats a scalar as a 1×1 array, so functions can naturally support array input and output when written properly.


    This page titled 3.11: Arrays as Function Arguments was last modified on Tue, 30 Jun 2026 02:11:55 GMT and is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Zara Noroozi.