Passing arrays to functions without declaring an array variable first

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Passing arrays to functions without declaring an array variable first

Stein Rørvik
Assuming a macro function accepting an array as a parameter:

function printArray(a) {
                print("");
                for (i=0; i<a.length; i++)
                               print(i+": "+a[i]);
}

What is the reason this works:

arr = newArray("cat", "dog", "mouse", "rabbit");
printArray(arr);

But not this:

printArray(newArray("cat", "dog", "mouse", "rabbit"));

?

Stein

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Passing arrays to functions without declaring an array variable first

Herbie
Good day Stein,

in your second example there is no name of the array but there need to be
one because the array is "in a certain sense returned" after the execution
of the function. In your second example there is no variable for the
"returned" array.

HTH

Herbie

::::::::::::::::::::::::::::::::::::::::::

Assuming a macro function accepting an array as a parameter:

function printArray(a) {
                print("");
                for (i=0; i<a.length; i++)
                               print(i+&quot;: &quot;+a[i]);
}

What is the reason this works:

arr = newArray(&quot;cat&quot;, &quot;dog&quot;, &quot;mouse&quot;,
&quot;rabbit&quot;);
printArray(arr);

But not this:

printArray(newArray(&quot;cat&quot;, &quot;dog&quot;, &quot;mouse&quot;,
&quot;rabbit&quot;));

?

Stein

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
&lt;/quote>




--
Sent from: http://imagej.1557.x6.nabble.com/

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Passing arrays to functions without declaring an array variable first

Wayne Rasband-2
In reply to this post by Stein Rørvik
> On Nov 23, 2019, at 1:07 PM, Stein Rørvik <[hidden email]> wrote:
>
> Assuming a macro function accepting an array as a parameter:

The latest ImageJ daily build (1,52s59) fixes a bug in the macro interpreter that prevented built in array functions like newArray(), split() and getProfile() from being used as arguments when calling user-defined functions.

-wayne


> function printArray(a) {
>                print("");
>                for (i=0; i<a.length; i++)
>                               print(i+": "+a[i]);
> }
>
> What is the reason this works:
>
> arr = newArray("cat", "dog", "mouse", "rabbit");
> printArray(arr);
>
> But not this:
>
> printArray(newArray("cat", "dog", "mouse", "rabbit"));
>
> ?
>
> Stein

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html