Most of the programs we have created in the first five chapters are relatively short and compact, focused on solving problems that are quite manageable. These pieces of code are often sufficient when working on a problem and you are looking for a quick result. But before long, something more is needed: a series of computations that have to be repeated perhaps, with slight variations each time; or you need to share your code with colleagues or students; or perhaps you are developing an application for a client who knows little of the Mathematica environment or language and is looking for a solution that integrates with what they are already comfortable with.
Whether you are writing code for yourself or others, it is important to develop programs that are not dependent upon the current state of the user's Mathematica session: previously defined symbols should not interfere with code in any way; side-effects of an evaluation should not cause surprises; programs should respond gracefully when things go wrong. All modern programming languages have frameworks designed to deal with these things, and Mathematica is no exception. This chapter introduces these structures and incorporates them throughout the rest of the book.
The first construct we will look at is localization of variables. This is a mechanism to isolate symbols and definitions that are local to a function in order to keep them from interfering with, or being interfered by, global symbols.
Providing options to functions and issuing messages when things go wrong are common mechanisms for getting your programs to look and behave like built-in functions; they are introduced in Section 6.2.
The chapter concludes with several larger pieces of code that put many of these pieces together in one program.
To save this book to your Kindle, first ensure [email protected] is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.
Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.
Find out more about the Kindle Personal Document Service.
To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.
To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.