returning array from a function

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

returning array from a function

dcsaba
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
Reply | Threaded
Open this post in threaded view
|

Re: returning array from a function

Lachlan Whitehead
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
Reply | Threaded
Open this post in threaded view
|

Re: returning array from a function

Krs5
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
Reply | Threaded
Open this post in threaded view
|

Re: returning array from a function

dcsaba
Hi Lachie,
thank you very much, it works perfectly. 

Could you send my a link, where I could read about the "why" which you did not want to explain in details, and about other "secrets" of imageJ macro language? 


Csaba



On Friday, November 8, 2013 10:00 AM, "Straatman, Kees R. (Dr.) [via ImageJ]" <[hidden email]> wrote:
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



If you reply to this email, your message will be added to the discussion below:
http://imagej.1557.x6.nabble.com/returning-array-from-a-function-tp5005495p5005497.html
To unsubscribe from returning array from a function, click here.
NAML


Reply | Threaded
Open this post in threaded view
|

Re: returning array from a function

Herbie-3
In reply to this post by dcsaba
Csaba,

why don't you use the variables a and b instead of n and m in your function?

HTH

Herbie
_____________________
On 08.11.13 08:48, dcsaba wrote:

> 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
>

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

Re: returning array from a function

Lachlan Whitehead
In reply to this post by dcsaba
Hi Csaba,

I don't really have a good source, but the convention in the programming that I'm familiar with is that the only thing a function will pass back to the rest of the code is its returned value. All variables within the function are not stored once the function has finished. A good habit to get into is not giving variables inside functions the same name as those outside as this can lead to significant confusion.

 For example the following code:

   a=1;
   print("a = " + a);
   b = change_a();
   print("a = " + a);

   function change_a(){
      a = 14;
      print("a = " + a);
      return a;
   }

Will give the following output:
a = 1   (the original definition)
a = 14  (the definition within the function)
a = 1   (back to the original when the function has exited)

Note that I've used the function to define the variable "b" as the returned value (14). If I didn't, the returned value doesn't get stored anywhere. This is where your code got into dangerous territory because you have a value defined within the macro which you want to alter (mtx) and one inside the function also called mtx, when the function version of mtx gets altered, it has no effect on the mtx in the rest of the macro.

Herbie also raised a good point to do with your example in that you passed the variables n and m to the function and stored them in a and b, yet your function still refers to n and m. In your example this is fine, but if you changed the code such that n and m didn't exist the function wouldn't work any more. A cleaned version of your example might look like this:

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

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


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

Note that now the function appl shares no common variables with the rest of the code. a,b and dummy_mtx are defined when calling the function, and forgotten once the function exits.

I hope that gives you some idea of what's going on, in terms of "secrets" of imageJ macros, I'm not sure. I just make it up as I go along and wait for it to break :)

Thanks,
-Lachie

----- Original Message -----
From: "dcsaba" <[hidden email]>
To: [hidden email]
Sent: Friday, 8 November, 2013 9:13:20 PM
Subject: Re: returning array from a function

Hi Lachie,
thank you very much, it works perfectly. 

Could you send my a link, where I could read about the "why" which you did not want to explain in details, and about other "secrets" of imageJ macro language? 


Csaba




On Friday, November 8, 2013 10:00 AM, "Straatman, Kees R. (Dr.) [via ImageJ]" <[hidden email]> wrote:
 
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


________________________________
 
If you reply to this email, your message will be added to the discussion below:http://imagej.1557.x6.nabble.com/returning-array-from-a-function-tp5005495p5005497.html 
To unsubscribe from returning array from a function, click here.
NAML



--
View this message in context: http://imagej.1557.x6.nabble.com/returning-array-from-a-function-tp5005495p5005498.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
Reply | Threaded
Open this post in threaded view
|

Re: returning array from a function

dcsaba
Hi Lachie, 
thank you for the explanation. It would be great to have a teacher like you around here.
Csaba


On Saturday, November 9, 2013 9:26 AM, Lachlan Whitehead [via ImageJ] <[hidden email]> wrote:
Hi Csaba,

I don't really have a good source, but the convention in the programming that I'm familiar with is that the only thing a function will pass back to the rest of the code is its returned value. All variables within the function are not stored once the function has finished. A good habit to get into is not giving variables inside functions the same name as those outside as this can lead to significant confusion.

 For example the following code:

   a=1;
   print("a = " + a);
   b = change_a();
   print("a = " + a);

   function change_a(){
      a = 14;
      print("a = " + a);
      return a;
   }

Will give the following output:
a = 1   (the original definition)
a = 14  (the definition within the function)
a = 1   (back to the original when the function has exited)

Note that I've used the function to define the variable "b" as the returned value (14). If I didn't, the returned value doesn't get stored anywhere. This is where your code got into dangerous territory because you have a value defined within the macro which you want to alter (mtx) and one inside the function also called mtx, when the function version of mtx gets altered, it has no effect on the mtx in the rest of the macro.

Herbie also raised a good point to do with your example in that you passed the variables n and m to the function and stored them in a and b, yet your function still refers to n and m. In your example this is fine, but if you changed the code such that n and m didn't exist the function wouldn't work any more. A cleaned version of your example might look like this:

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

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


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

Note that now the function appl shares no common variables with the rest of the code. a,b and dummy_mtx are defined when calling the function, and forgotten once the function exits.

I hope that gives you some idea of what's going on, in terms of "secrets" of imageJ macros, I'm not sure. I just make it up as I go along and wait for it to break :)

Thanks,
-Lachie

----- Original Message -----
From: "dcsaba" <[hidden email]>
To: [hidden email]
Sent: Friday, 8 November, 2013 9:13:20 PM
Subject: Re: returning array from a function

Hi Lachie,
thank you very much, it works perfectly. 

Could you send my a link, where I could read about the "why" which you did not want to explain in details, and about other "secrets" of imageJ macro language? 


Csaba




On Friday, November 8, 2013 10:00 AM, "Straatman, Kees R. (Dr.) [via ImageJ]" <[hidden email]> wrote:
 
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


________________________________
 
If you reply to this email, your message will be added to the discussion below:http://imagej.1557.x6.nabble.com/returning-array-from-a-function-tp5005495p5005497.html 
To unsubscribe from returning array from a function, click here.
NAML



--
View this message in context: http://imagej.1557.x6.nabble.com/returning-array-from-a-function-tp5005495p5005498.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



If you reply to this email, your message will be added to the discussion below:
http://imagej.1557.x6.nabble.com/returning-array-from-a-function-tp5005495p5005517.html
To unsubscribe from returning array from a function, click here.
NAML