Login  Register

Re: behavior of variable when function is called?

Posted by Fred Damen on Jan 07, 2021; 10:37pm
URL: http://imagej.273.s1.nabble.com/behavior-of-variable-when-function-is-called-tp5024336p5024339.html

Greetings Michael,

There is more than one variable called 'c'.  Each scope, roughly between
squiggly brackets (at least in the C/C++ derived languages), has its own
context containing a symbol table with variable / value pairs.  From the
output of the attached macro it seems as though the called function's
context is initialized by the context of its caller.  Although, it does
not seem to copy the values back to the calling context, which which can
create nasty headaches. Each invocation of a scope seems to reuse its
context and thus the increment performs as shown.

Fred

macro "UGH" {
   c = 10;
   prefun();
   for(i=0; i<5; i++)
      fun();
   fun()
   }

function prefun() {
   c=100;
   print("C="+c);
   prefun2();
   }

function prefun2() {
   print("C = "+c);
   c=1000;
   print("C="+c);
   }

function fun() {
   print("c="+c++);
   }

output...
C=100
C = 100
C=1000
c=10
c=11
c=12
c=13
c=14
c=15




On Thu, January 7, 2021 3:14 pm, Cammer, Michael wrote:

> I need to rename hundreds of files in nested folders and using the model
> of the macro at https://imagej.nih.gov/ij/macros/ListFilesRecursively.txt
> , have generated code that does what we need.
>
> I am confused by one issue, however, that c keeps incrementing.
>
> In our implementation (below), c is defined in a macro and is not passed
> to the function.  c is not a global variable.  When the function calls
> itself, it does not pass c.
>
> I thought the function would expect that either c had to be passed to it
> or would require a new instance to be defined before it could be
> incremented.
>
> Is this something I have been overlooking for years while diligently
> writing my code to pass all values needed by a function?
>
> Best regards-
> Michael
>
>
> macro "replace parts of file names recursively" {
>   dir = getDirectory("Choose a Directory ");
>   count = 1;
>   listFiles(dir);
> }
>
>   function listFiles(path) {
>      list = getFileList(path);
>      for (i=0; i<list.length; i++) {
>         if (endsWith(list[i], "/"))
>            listFiles(""+path+list[i]);
>         else
>            {
>                path1 = path + list[i];
>                                 title_stripped = list[i];     // leave the
> tif extension on
>                                                 // clean up other likely
> issues in title
>                                                 title_stripped =
> replace(title_stripped,
> "-MaxIP", "max");
>                                                 title_stripped =
> replace(title_stripped, "-
> C=0", "");
>                                                 title_stripped =
> replace(title_stripped,
> "gamma", "gam");
>                                                 path2 = path +
> title_stripped;
>                                                 ok = File.rename(path1,
> path2);                 //
> ok = 1 if successful
>                                                 print((count++) + ": " +
> path + list[i]);
>                }
>
>      }
>   }
>
>
>
>
> Michael Cammer, Sr Research Scientist, DART Microscopy Laboratory
> NYU Langone Health, 540 First Avenue, SK2 Microscopy Suite, New York, NY
> 10016
> Office: 646-501-0567 Cell: 914-309-3270
> [hidden email]<mailto:[hidden email]>
> http://nyulmc.org/micros  http://microscopynotes.com/
> Acknowledgement in your publications and presentations of work performed
> in the Microscopy Core plays a vital role in securing support and the
> funding necessary to maintain and operate this valuable research resource.
>   For publications that were made possible by work performed in the core,
> please use the acknowledgement statement "We thank the NYU Langone
> Microscopy Core for experimental and technical support" and include
> required grant numbers as listed here
> http://microscopynotes.com/ilabnyu/acknowledgements2017.pdf
> Please also consider staff for co-authorship if they played a key role in
> the study.
>
>
>
> ------------------------------------------------------------
> This email message, including any attachments, is for the sole use of the
> intended recipient(s) and may contain information that is proprietary,
> confidential, and exempt from disclosure under applicable law. Any
> unauthorized review, use, disclosure, or distribution is prohibited. If
> you have received this email in error please notify the sender by return
> email and delete the original message. Please note, the recipient should
> check this email and any attachments for the presence of viruses. The
> organization accepts no liability for any damage caused by any virus
> transmitted by this email.
> =================================
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

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