help-octave
[Top][All Lists]
Advanced

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

Re: Functions inhereting variables


From: Moo
Subject: Re: Functions inhereting variables
Date: Thu, 18 Nov 2010 21:09:55 -0700

On Thu, Nov 18, 2010 at 7:09 PM, Mike B. <address@hidden> 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.

Another option is to  make a and b global variables, like so:

function out = f( x )
  global a; global b;
  out = x + a + b;
endfunction

and in the main code:
global a; global b;
a=1
b=2
f( 3 )

Note that you have put "global a; global b;" in both the function file and the script otherwise it won't work.

reply via email to

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