returning string from function

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

returning string from function

mcpeek
Hi,

I am trying to return a string variable from a function to the main body of a macro.  The string variable has the right value inside the function, but when it is passed back to the macro body, it has a value of 0.

I'm new to ImageJ, so I thought that maybe strings might be considered objects in some way (e.g., arrays of characters), and so needed to be passed by reference as with variables declared as arrays from the start.  This didn't work either.

Any insight as to why this won't work, and what will make it work, would be greatly appreciated.  A little test macro is below, using the get time test macro as a function.  

Thanks,

Mark

*** Code to Follow ***


macro "test" {
     timeString = "";
     timeString = getTime();
     showMessage(timeString);
}

function getTime() {
     MonthNames = newArray("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
     DayNames = newArray("Sun", "Mon","Tue","Wed","Thu","Fri","Sat");
     getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute, second, msec);
     TimeString ="Date: "+DayNames[dayOfWeek]+" ";
     if (dayOfMonth<10) {TimeString = TimeString+"0";}
     TimeString = TimeString+dayOfMonth+"-"+MonthNames[month]+"-"+year+"\nTime: ";
     if (hour<10) {TimeString = TimeString+"0";}
     TimeString = TimeString+hour+":";
     if (minute<10) {TimeString = TimeString+"0";}
     TimeString = TimeString+minute+":";
     if (second<10) {TimeString = TimeString+"0";}
     TimeString = TimeString+second;

     showMessage(TimeString);
     return(TimeString);
}


Reply | Threaded
Open this post in threaded view
|

Re: returning string from function

David Webster
Mark,

If you use "return TimeString;"  instead of  "return(TimeString)", it works.

return(TimeString) isn't the correct syntax. I think the macro porocesor is
interpreting as a zero. If you do

x = (TimeString); return x; then you get a zero.

David




On Tue, Aug 10, 2010 at 5:33 PM, mcpeek <[hidden email]> wrote:

> Hi,
>
> I am trying to return a string variable from a function to the main body of
> a macro.  The string variable has the right value inside the function, but
> when it is passed back to the macro body, it has a value of 0.
>
> I'm new to ImageJ, so I thought that maybe strings might be considered
> objects in some way (e.g., arrays of characters), and so needed to be
> passed
> by reference as with variables declared as arrays from the start.  This
> didn't work either.
>
> Any insight as to why this won't work, and what will make it work, would be
> greatly appreciated.  A little test macro is below, using the get time test
> macro as a function.
>
> Thanks,
>
> Mark
>
> *** Code to Follow ***
>
>
> macro "test" {
>     timeString = "";
>     timeString = getTime();
>     showMessage(timeString);
> }
>
> function getTime() {
>     MonthNames =
>
> newArray("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
>     DayNames = newArray("Sun", "Mon","Tue","Wed","Thu","Fri","Sat");
>     getDateAndTime(year, month, dayOfWeek, dayOfMonth, hour, minute,
> second, msec);
>     TimeString ="Date: "+DayNames[dayOfWeek]+" ";
>     if (dayOfMonth<10) {TimeString = TimeString+"0";}
>     TimeString =
> TimeString+dayOfMonth+"-"+MonthNames[month]+"-"+year+"\nTime: ";
>     if (hour<10) {TimeString = TimeString+"0";}
>     TimeString = TimeString+hour+":";
>     if (minute<10) {TimeString = TimeString+"0";}
>     TimeString = TimeString+minute+":";
>     if (second<10) {TimeString = TimeString+"0";}
>     TimeString = TimeString+second;
>
>     showMessage(TimeString);
>     return(TimeString);
> }
>
>
>
> --
> View this message in context:
> http://imagej.588099.n2.nabble.com/returning-string-from-function-tp5410554p5410554.html
> Sent from the ImageJ mailing list archive at Nabble.com.
>
Reply | Threaded
Open this post in threaded view
|

Re: returning string from function

mcpeek
On 8/10/2010 9:22 PM, David William Webster [via ImageJ] wrote:
Mark,

If you use "return TimeString;"  instead of  "return(TimeString)", it works.

return(TimeString) isn't the correct syntax. I think the macro porocesor is
interpreting as a zero. If you do

x = (TimeString); return x; then you get a zero.

David


Thanks David.

Still getting used to ImageJ.