We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Returning a float from a function
Page Index Toggle Pages: 1
Returning a float from a function (Read 1050 times)
Returning a float from a function
Sep 17th, 2009, 4:31am
 
I'm just getting started with Processing and am having trouble creating a function to compute the haversine of an angle. The function needs to take the angle (float) and return the haversine (float).

I've created the following simple static program to test the function, but I receive "unexpected token: float" with the function declaration highlighted. Am I doing something daft, or just completely off base?

Code:
println(haversine(PI/2));


float haversine(float haverAngle) {
 float haverReturn=0;
 haverReturn=(0.5*(1-cos(haverAngle))); //Definition from Wolfram
 return(haverReturn);
}
Re: Returning a float from a function
Reply #1 - Sep 17th, 2009, 4:58am
 
Try:

Code:
void setup() {
 println(haversine(PI/2));
}


float haversine(float haverAngle) {
 float haverReturn=0;
 haverReturn=(0.5*(1-cos(haverAngle))); //Definition from Wolfram
 return(haverReturn);
}


Not including setup() makes Processing run slightly differently.  This has been explained in the forums before; but I must admit I still haven't got my head round it entirely...  Basically it's better to include it, even for very basic sketches.  Also note that if you're setting 'size', the size statement should always be the first statement in setup()...
Re: Returning a float from a function
Reply #2 - Sep 17th, 2009, 5:08am
 
P.S.
I did some work with the Haversine formula recently in ActionScript and found this reference very useful and also this.
Re: Returning a float from a function
Reply #3 - Sep 17th, 2009, 5:12am
 
blindfish wrote on Sep 17th, 2009, 4:58am:
Not including setup() makes Processing run slightly differently.  This has been explained in the forums before; but I must admit I still haven't got my head round it entirely...

Simple: if you don't have a setup(), the pre-processor will put all your code in an invisible, generated setup(). So if you declare functions, you will get illegal code, as Java doesn't support functions inside functions.
So code without setup() must be quite linear...
Re: Returning a float from a function
Reply #4 - Sep 17th, 2009, 10:05am
 
That was it!  Actually pretty simple really - just that I had forgotten:  My memory is pretty bad these days...
Re: Returning a float from a function
Reply #5 - Sep 17th, 2009, 7:00pm
 
Thanks, including the short setup() made all the difference!

The following code works:

Code:
void setup() {
println(haversine(PI/2));
}

float haversine(float haverAngle){
 float haverReturn=0;
 haverReturn=(0.5*(1-cos(haverAngle))); //Definition from Wolfram
 return(haverReturn);
}



Thanks for the links too; I'm finding that even the venerable navigation text Bowditch is a bit short on some of the details of calculation for this project.
Page Index Toggle Pages: 1