15.11.2014 Views

Chapter 4: Programming in Matlab - College of the Redwoods

Chapter 4: Programming in Matlab - College of the Redwoods

Chapter 4: Programming in Matlab - College of the Redwoods

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

400 <strong>Chapter</strong> 4 <strong>Programm<strong>in</strong>g</strong> <strong>in</strong> <strong>Matlab</strong><br />

Variable Scope <strong>in</strong> Nested Functions<br />

So, what’s <strong>the</strong> big deal? Why should we bo<strong>the</strong>r to study nested functions? What<br />

do <strong>the</strong>y <strong>of</strong>fer that we don’t already have with <strong>the</strong> exist<strong>in</strong>g functions and subfunctions<br />

we’ve already studied?<br />

The answer lies <strong>in</strong> variable scope. As a short example, open <strong>the</strong> editor and<br />

enter <strong>the</strong> follow<strong>in</strong>g l<strong>in</strong>es. Save <strong>the</strong> function M-file as varScope1.m.<br />

function varScope1<br />

x=3;<br />

nestfun1<br />

function nestfun1<br />

fpr<strong>in</strong>tf(’In nestfun1, x equals: %d\n’,x)<br />

Note that we have a primary function named varScope1 which conta<strong>in</strong>s one<br />

subfunction named nestfun1. The primary assigns <strong>the</strong> variable x <strong>the</strong> scalar 3,<br />

<strong>the</strong>n calls <strong>the</strong> subfunction with <strong>the</strong> command nestfun1. In turn, <strong>the</strong> subfunction<br />

nestfun1 attempts to pr<strong>in</strong>t <strong>the</strong> value <strong>of</strong> <strong>the</strong> variable x fpr<strong>in</strong>tf with a nice format<br />

str<strong>in</strong>g.<br />

Remember that <strong>the</strong> primary function varScope1 and <strong>the</strong> subfunction nestfun1<br />

have separate workspaces. The variable x exists <strong>in</strong> <strong>the</strong> primary workspace, but<br />

<strong>the</strong> subfunction workspace is <strong>in</strong>dependent <strong>of</strong> <strong>the</strong> primary workspace and thus has<br />

no knowledge <strong>of</strong> <strong>the</strong> variable x. Thus, when we run <strong>the</strong> function, we should not<br />

be surprised by <strong>the</strong> error message.<br />

>> varScope1<br />

??? Undef<strong>in</strong>ed function or variable ’x’.<br />

Error <strong>in</strong> ==> varScope1>nestfun1 at 6<br />

fpr<strong>in</strong>tf(’In nestfun1, x equals: %d\n’,x)<br />

Error <strong>in</strong> ==> varScope1 at 3<br />

nestfun1<br />

This is precisely <strong>the</strong> error message we expected to see. The variable x exists <strong>in</strong><br />

<strong>the</strong> primary function workspace, but <strong>the</strong> subfunction workspace has no knowledge<br />

<strong>of</strong> <strong>the</strong> variable x. When <strong>the</strong> subfunction attempts to pr<strong>in</strong>t <strong>the</strong> contents <strong>of</strong> <strong>the</strong><br />

variable x, an error occurs.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!