Login  Register

Re: User-defined functions

Posted by Andy Weller on Aug 02, 2006; 5:35pm
URL: http://imagej.273.s1.nabble.com/User-defined-functions-tp3701916p3701919.html

Hi all,

(I will document the response to this on the Docs website soon.)

For some reason I can't set global variables outside my macro?! The
macro just doesn't start.

What if I want to return 2 variables instead of just the 1 - or is this
bad practise? (I guess I can call a function from within a function.)

macro "test"
{
x, y=doit();
print(x, y);
}

function doit()
{
x=10;
y=20;
return x, y;
}

?!?

Cheers, Andy

On Wed, 2006-08-02 at 15:56 +0200, seb wrote:

> Andy Weller wrote:
> > I have a user-defined function that does various measurements:
> >
> > function measurements() {
> > some measurements
> > ...
> > out = "";
> > out = Name + "," + area + "," + ...; // String container for all results
> > return out;
> > }
> >
> > Now in my main macro it states that "out" is an "undefined variable",
> > although I am returning it from function measurements()?!
> >
> > macro "Test" {
> > dir1 = getDirectory("Choose Source Directory");
> > list = getFileList(dir1);
> > numberFile = list.length;
> > setBatchMode(true);
> > compResults = newArray(numberFile);
> > for (i=0; i<numberFile; i++) {
> >    some stuff
> >    ...
> >    measurements();
> >    compResults[i] = out; // THIS IS WHERE IT BUMS OUT?!
> > }
>
> Hello Andy,
>
> this "out" is still undefined, even if your function returns a value
> (the "out" string it has just built), the "out" variable is not defined
> in the macro, it's a local variable in your function.
>
> just like this:
>
> macro "test"
> {
> //doit();//wrong: x is locally defined in doit(), not defined here.
> x=doit(); //correct
> print(x);
> }
>
> function doit()
> {
> x=10;
> return x;
> }
>
>
> If you need a global variable, you have to define it first before your
> macro
> var out="" ;
> ....
> macro "test"
> {
> ...
> print(out);
> }
>
> beware, the "out"  variable modified in your function call will now be
> the global one.
>
> Sebastien