[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Functions inhereting variables
From: |
Ben Abbott |
Subject: |
Re: Functions inhereting variables |
Date: |
Thu, 18 Nov 2010 21:57:37 -0500 |
On Nov 18, 2010, at 9:09 PM, Mike B. wrote:
> Hi all,
>
> Is it possible to define a function which `inherits' variables from the scope
> it was called from without explicity passing them for example:
>
> function out = f( x )
> out = x + a + b;
> endfunction
>
> and in the main code:
> a=1
> b=2
> f( 3 )
> would give 6
>
> Anonymous functions can inherit variables but they seem limited to simple
> 1-line codes.
>
> Thanks,
> Mike.
Look at "evalin"
help evalin
For your example ..
function out = f (x)
a = evalin ("caller", "a", NaN);
b = evalin ("caller", "b", NaN);
out = x + a + b;
endfunction
Ben