Programming is the process of writing a sequence of commands that a computer can follow to perform a task. A programming language, like MATLAB, provides a way to communicate your instructions to your computer.
When you move from experimenting in the command window to writing scripts and functions, you package commands into tasks and decide how those tasks should run. As a programmer, your job is to steer the flow of your program so it runs the correct commands at the right times.
Scripts execute statements from top to bottom by default, and functions bundle a sequence of tasks you can reuse. However, most programs need to make decisions and repeat actions based on conditions. This section discusses the different flow patterns of code and the ways you can control them.
The file name should match the function name. If the function header is function totalCents = coin_total(...), then the file should be named coin_total.m.
Scripts vs Functions. Scripts and functions are both .m files that execute MATLAB commands from top to bottom. Their differences are summarized in TableΒ 15.