Login  Register

Re: returning array from a function

Posted by Krs5 on Nov 08, 2013; 8:59am
URL: http://imagej.273.s1.nabble.com/returning-array-from-a-function-tp5005495p5005497.html

As well as an array starts to count at 0 (that is why you get the error out of range (0-3)), so your line k=mtx[4]; should be k=mtx[3];

Kees

-----Original Message-----
From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of Lachlan Whitehead
Sent: 08 November 2013 08:52
To: [hidden email]
Subject: Re: returning array from a function

Hi Csaba,

Without getting into the details of why, it's because variables within functions are only stored inside functions.
All you need to do in this example is re-define the variable as the output of the function.

mtx=appl(n,m,mtx);

-Lachie




----- Original Message -----
From: "dcsaba" <[hidden email]>
To: [hidden email]
Sent: Friday, 8 November, 2013 6:48:24 PM
Subject: returning array from a function

I have tried to modify an array in a function, so i wrote this macro and
function:

macro "func_test" {
        n=3;
        m=6;
        mtx=newArray(1,2,3,4);
        appl(n,m, mtx);

        Array.print(mtx);
        k=mtx[4];
        print(k);
}


function appl(a,b, mtx) {
       
        Array.print(mtx);
        mtx=Array.concat(mtx, n);
        Array.print(mtx);
        mtx=Array.concat(mtx, m);
        Array.print(mtx);
       
        return mtx;
       
        }


What I expected is that the array mtx would contain 1, 2, 3, 4, 3, 6 after the function was executed. Instead, I saw that the array was printed from the function as i expected, but the returned value contained  the original values only. And I was also unable to acces the array position 4 (mtx[4]), and i got a massege thet it was out of range (0-3).
I have searched the net for a solution for 2 whole days and i have fount nothing that could explain this annoying behaviour of the function.

PLEASE, PLEASE, PLEASE HELP MEEEEE!

How is it possible to return the modified array, and how can i access the new values at the end of the array??????

Many thanks in advance!
Csaba




--
View this message in context: http://imagej.1557.x6.nabble.com/returning-array-from-a-function-tp5005495.html
Sent from the ImageJ mailing list archive at Nabble.com.

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

______________________________________________________________________
The information in this email is confidential and intended solely for the addressee.
You must not disclose, forward, print or use it without the permission of the sender.
______________________________________________________________________

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

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