<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Nested Functions and Variable Scope</title>
	<atom:link href="http://blogs.mathworks.com/loren/2008/01/16/nested-functions-and-variable-scope/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2008/01/16/nested-functions-and-variable-scope/</link>
	<description>Loren Shure works on design of the MATLAB language at MathWorks. She writes here about once a week on MATLAB programming and related topics.</description>
	<lastBuildDate>Mon, 13 Feb 2012 13:24:10 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2008/01/16/nested-functions-and-variable-scope/#comment-31829</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Wed, 10 Nov 2010 15:03:27 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/01/16/nested-functions-and-variable-scope/#comment-31829</guid>
		<description>Jung-

You did not write a nested function but a subfunction.  yoyo2 is not nested inside test.

--Loren</description>
		<content:encoded><![CDATA[<p>Jung-</p>
<p>You did not write a nested function but a subfunction.  yoyo2 is not nested inside test.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jung</title>
		<link>http://blogs.mathworks.com/loren/2008/01/16/nested-functions-and-variable-scope/#comment-31828</link>
		<dc:creator>Jung</dc:creator>
		<pubDate>Wed, 10 Nov 2010 14:35:28 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/01/16/nested-functions-and-variable-scope/#comment-31828</guid>
		<description>Sorry Loren if the question is very basic. When I run the following, I get data=1 when it should be data=3. Am I missing something basic here?

&lt;pre&gt;

function test(3)
 data = 1;
 yoyo2(3);
 data
end

function yoyo2(in)
 data = in;
end

&lt;/pre&gt;

Thanks,
Jung.</description>
		<content:encoded><![CDATA[<p>Sorry Loren if the question is very basic. When I run the following, I get data=1 when it should be data=3. Am I missing something basic here?</p>
<pre>

function test(3)
 data = 1;
 yoyo2(3);
 data
end

function yoyo2(in)
 data = in;
end
</pre>
<p>Thanks,<br />
Jung.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2008/01/16/nested-functions-and-variable-scope/#comment-31409</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Wed, 09 Jun 2010 14:16:21 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/01/16/nested-functions-and-variable-scope/#comment-31409</guid>
		<description>A concerned programmer-

Thanks for your concerns. FYI, you can guarantee, in a nested function, that variables are local if you list them in the input or output argument list. 

--Loren</description>
		<content:encoded><![CDATA[<p>A concerned programmer-</p>
<p>Thanks for your concerns. FYI, you can guarantee, in a nested function, that variables are local if you list them in the input or output argument list. </p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: a concerned programmer</title>
		<link>http://blogs.mathworks.com/loren/2008/01/16/nested-functions-and-variable-scope/#comment-31408</link>
		<dc:creator>a concerned programmer</dc:creator>
		<pubDate>Wed, 09 Jun 2010 14:13:44 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/01/16/nested-functions-and-variable-scope/#comment-31408</guid>
		<description>Matlab really should recognize the &#039;local&#039; keyword in order to force a variable in a nested function to be local. Otherwise, there is no way to gaurantee that a variable is has local scope unless all functions are declared in separate files, which is not always the best solution, and actually kind of annoying to work with in some cases.

Just a suggestion.</description>
		<content:encoded><![CDATA[<p>Matlab really should recognize the &#8216;local&#8217; keyword in order to force a variable in a nested function to be local. Otherwise, there is no way to gaurantee that a variable is has local scope unless all functions are declared in separate files, which is not always the best solution, and actually kind of annoying to work with in some cases.</p>
<p>Just a suggestion.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: OysterEngineer</title>
		<link>http://blogs.mathworks.com/loren/2008/01/16/nested-functions-and-variable-scope/#comment-30905</link>
		<dc:creator>OysterEngineer</dc:creator>
		<pubDate>Fri, 18 Dec 2009 15:16:45 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/01/16/nested-functions-and-variable-scope/#comment-30905</guid>
		<description>Yes, that is kind of what I did.

However, I was hoping to find something like the codemetrics that would summarize the nesting relationship.  I couldn&#039;t find anything on the file exchange, so I ended up doing it by hand &amp; making a table in an Excel spreadsheet.</description>
		<content:encoded><![CDATA[<p>Yes, that is kind of what I did.</p>
<p>However, I was hoping to find something like the codemetrics that would summarize the nesting relationship.  I couldn&#8217;t find anything on the file exchange, so I ended up doing it by hand &amp; making a table in an Excel spreadsheet.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2008/01/16/nested-functions-and-variable-scope/#comment-30904</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Thu, 17 Dec 2009 19:44:20 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/01/16/nested-functions-and-variable-scope/#comment-30904</guid>
		<description>OysterEngineer-

See if this helps.  Put a break point in one of the nested functions, perhaps at the last executable statement.  Type &quot;who&quot; at the MATLAB prompt.  The display should show you local variables and up-level ones.  If this helps, you can then do that for each of the nested functions.  Tedious, yes, but I think it will get you the info you are looking for.

--Loren</description>
		<content:encoded><![CDATA[<p>OysterEngineer-</p>
<p>See if this helps.  Put a break point in one of the nested functions, perhaps at the last executable statement.  Type &#8220;who&#8221; at the MATLAB prompt.  The display should show you local variables and up-level ones.  If this helps, you can then do that for each of the nested functions.  Tedious, yes, but I think it will get you the info you are looking for.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: OysterEngineer</title>
		<link>http://blogs.mathworks.com/loren/2008/01/16/nested-functions-and-variable-scope/#comment-30903</link>
		<dc:creator>OysterEngineer</dc:creator>
		<pubDate>Thu, 17 Dec 2009 19:40:15 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/01/16/nested-functions-and-variable-scope/#comment-30903</guid>
		<description>I&#039;ve inherited some complex .m code, written by a very competent co-worker.  The code has ~40 nested functions &amp; I&#039;m struggling with the very issue of which variables each nested function can see or has revised.

I just wish there was some simple way to list all the variables a nested function could see.  That would make de-bugging much easier.</description>
		<content:encoded><![CDATA[<p>I&#8217;ve inherited some complex .m code, written by a very competent co-worker.  The code has ~40 nested functions &amp; I&#8217;m struggling with the very issue of which variables each nested function can see or has revised.</p>
<p>I just wish there was some simple way to list all the variables a nested function could see.  That would make de-bugging much easier.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alessandro</title>
		<link>http://blogs.mathworks.com/loren/2008/01/16/nested-functions-and-variable-scope/#comment-30639</link>
		<dc:creator>Alessandro</dc:creator>
		<pubDate>Wed, 23 Sep 2009 14:07:06 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/01/16/nested-functions-and-variable-scope/#comment-30639</guid>
		<description>Tristan, try with this experiment:
&lt;pre&gt;
function test1
x=ones(10000,10000);
tic
for i=1:100
x = tsqr(x);
end
toc

end

function a = tsqr(x)
a=(x+1)./x;
end
&lt;/pre&gt;
vs.
&lt;pre&gt;
function test2
x=ones(10000,10000);
tic
for i=1:100
tsqr;
end
toc



    function  tsqr
        x =(x+1)./x;
    end

end
&lt;/pre&gt;

the first is slower(try with 1000) and gives out of memory error (with 10000)!</description>
		<content:encoded><![CDATA[<p>Tristan, try with this experiment:</p>
<pre>
function test1
x=ones(10000,10000);
tic
for i=1:100
x = tsqr(x);
end
toc

end

function a = tsqr(x)
a=(x+1)./x;
end
</pre>
<p>vs.</p>
<pre>
function test2
x=ones(10000,10000);
tic
for i=1:100
tsqr;
end
toc

    function  tsqr
        x =(x+1)./x;
    end

end
</pre>
<p>the first is slower(try with 1000) and gives out of memory error (with 10000)!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tristan</title>
		<link>http://blogs.mathworks.com/loren/2008/01/16/nested-functions-and-variable-scope/#comment-29107</link>
		<dc:creator>Tristan</dc:creator>
		<pubDate>Fri, 16 May 2008 14:13:24 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/01/16/nested-functions-and-variable-scope/#comment-29107</guid>
		<description>yeah you are right, one example dont prove anything.

It just shows that, in some cases, there might be some important speed differences between a function and the same one nested, or using global variables.</description>
		<content:encoded><![CDATA[<p>yeah you are right, one example dont prove anything.</p>
<p>It just shows that, in some cases, there might be some important speed differences between a function and the same one nested, or using global variables.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2008/01/16/nested-functions-and-variable-scope/#comment-28873</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Wed, 07 May 2008 11:13:17 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/2008/01/16/nested-functions-and-variable-scope/#comment-28873</guid>
		<description>Tristan-

Nested functions can be slower in some cases currently.  We know we have some opportunities to optimize more.  However, be very careful as it depends on how many variables, how much data there is, how much data changes, etc.  I don&#039;t think one experiment characterizes the larger picture fully.

--Loren</description>
		<content:encoded><![CDATA[<p>Tristan-</p>
<p>Nested functions can be slower in some cases currently.  We know we have some opportunities to optimize more.  However, be very careful as it depends on how many variables, how much data there is, how much data changes, etc.  I don&#8217;t think one experiment characterizes the larger picture fully.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
</channel>
</rss>

