2.7: Scripts vs. Functions

Fundamentals of MATLAB Programming · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    Read aloud

    How

    Speed: 1×
    Light up
    Highlight

    Symbols: Mulberry Symbols, © 2018–2026 Steve Lee, CC BY-SA 4.0 (licence, the set), the site's own copies.

    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.

    2.7: Scripts vs. Functions

    A script is a file that contains a sequence of commands. It can use variables from the Workspace and it can also create variables in the Workspace. Scripts are useful for organizing a complete task, especially when the task includes input, calculations, and output.

    A function is different. A function is a reusable block of code designed to perform a specific job. It usually receives input arguments, performs a calculation, and returns output arguments. Functions are especially useful when you want to reuse the same calculation many times.

    Feature

    Script

    Function

    Purpose

    Runs a sequence of commands for a task

    Performs a specific reusable calculation

    File extension

    .m

    .m

    Inputs

    Often from input statements or workspace variables

    Usually from input arguments

    Outputs

    Often displayed or stored in workspace variables

    Usually returned as output arguments

    Best use

    A full program or workflow

    A reusable formula or operation


    This page titled 2.7: Scripts vs. Functions was last modified on Tue, 30 Jun 2026 02:11:45 GMT and is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Zara Noroozi.