octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Functions in scripts


From: Rik
Subject: Re: Functions in scripts
Date: Tue, 24 Jan 2017 11:52:15 -0800

On 01/24/2017 11:17 AM, Nicholas Jankowski wrote:

What does this return in Matlab?

%%%%%%%%%%%%
x = 1;

localfunctions ()

z = f(x)

localfunctions ()

function y = f(x)
y = 2 * x;
end
%%%%%%%%%%%%

I'm interested in when Matlab adds the function to the symbol table, and Mike's question is whether it stays in the symbol table after the script finishes.

--Rik

>> testscript
ans =
  cell
    @f
z =
     2
ans =
  cell
    @f



Thanks.  So Matlab is reading through the entire script once and accepting forward declarations.  There should be a way to accomplish the same thing in Octave because Octave already knows how to do this for function files and forward declarations of subfunctions.  The following function in a file script3.m does that

%%%%%%%%%%%%%
function script3 ()
x = 1;

localfunctions ()

z = f(x)

end

function y = f(x)
y = 2 * x;
end
%%%%%%%%%%%%%

And running it produces

%%%%%%%%%%%%
ans =
{
  [1,1] = @f
}
z =  2
%%%%%%%%%%%%

Mike's questions still remains.  After running the original script, what does

which f

return?

If it is empty then the function is localized to the script.

--Rik

reply via email to

[Prev in Thread] Current Thread [Next in Thread]