Opening the book…
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.
The book could not be loaded just now.