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 › Want to return multiple items from a function
Page Index Toggle Pages: 1
Want to return multiple items from a function (Read 1182 times)
Want to return multiple items from a function
Nov 17th, 2009, 1:53pm
 
Goal: Create a function that returns  x & y coordinates based on the circumference of a circle.

Problem:
Can't get the function to return more than one data point when I need two. Get error message. "Syntax error, maybe a missing semicolon?

Code:
void setup(){
//some code here
}

int placeOnCirc (int circWide, int angle){

 int x = int( cos(radians(angle)) * (circWide/2));
 int y = int( sin(radians(angle)) * (circWide/2));

 return x, y;
}

Can someone tell me what I need to do to return an x AND y coordinate? I assume it involves an array but don't know how.

Thanks.
Re: Want to return multiple items from a function
Reply #1 - Nov 17th, 2009, 2:27pm
 
While you could use a two element array, it'd probably be clearer if you used an object.  Processing has a class called PVector which is often used to describe locations (or velocities, accelerations, etc.).

What you should do is make your function return PVector rather than int, and then return a PVector that contains both the x and y values.

Your new function might look something like this:
Code:
PVector placeOnCirc (int circWide, int angle) {
 PVector location = new PVector();

 location.x = cos(radians(angle)) * (circWide/2);
 location.y = sin(radians(angle)) * (circWide/2);

 return location;
}


Note that the PVector class uses floats rather than ints for its values, though for this function that shouldn't be a problem.

For more information about the PVector class, see this reference page:

http://processing.org/reference/PVector.html
Re: Want to return multiple items from a function
Reply #2 - Nov 17th, 2009, 4:51pm
 
Thanks. Really appreciate it.
Page Index Toggle Pages: 1