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 › no parameter-passing
Page Index Toggle Pages: 1
no parameter-passing? (Read 266 times)
no parameter-passing?
Mar 2nd, 2009, 10:38pm
 
I come from Arduino, and I just got used to using pointers for parameter passing between functions. It seems those heydays are over now - at least I can't make it work in Processing. Is it really gone? And what do I do then? Maintain a heap of global variables?
Re: no parameter-passing?
Reply #1 - Mar 2nd, 2009, 11:18pm
 
Arduino use C? Or C++?
In Java, there are no pointers, all parameters to functions are passed by value.
But when you pass an object, the value is actually a reference to the object, so it is a bit like passing a pointer to the object.
Re: no parameter-passing?
Reply #2 - Mar 3rd, 2009, 9:52am
 
Arduino is C++ (but my humble guess is that most people only use the C part of the language)

I have two variables that I would like to pass to my function. How do I then make Processing believe that what I pass is objects?
Re: no parameter-passing?
Reply #3 - Mar 3rd, 2009, 10:55am
 
Excuse me, but your question is a bit too generic.
What are your variables, their type?
What your function is supposed to do?
Re: no parameter-passing?
Reply #4 - Mar 3rd, 2009, 1:12pm
 
void setup() {
 int x-coordinate = 10, y-coordinate = 10;
}

void draw() {
 moveBoat(x-coordinate, y-coordinate);
}

moveBoat(int x, int y) {
 //move boat and
 //update boat coordinates
}
Re: no parameter-passing?
Reply #5 - Mar 3rd, 2009, 3:35pm
 
First, you can't use dash in variable names (ambiguity with minus operator).
Next, indeed there is several ways to do this.
Simple way, as you point it: declare coordinates at global level, update everywhere. That's OK if you have only one boat, it can be messy with lot of stuff. Although you can group them in an array or an ArrayList.
Actually, you need to do that, so setup() can communicate with draw(): both are part of the same (hidden) higher level object (PApplet), and so access this object's members (so called global variables).

You can use objects to avoid having too many atomic variables. Either a simple point object, to pass around coordinates, or a more sophisticated boat object, knowing where it is, what it is (size, color, etc.) and how to draw itself.

To illustrate the simple object:

PVector boatPos1, boatPos2; // PVector is Processing's class to handle pairs of coordinates

void setup() {
 // Init
 boatPos1 = new PVector(10, 10);
 boatPos2 = new PVector(width / 2, 10);
}

void draw() {
 // Move all the boats
 moveBoat(boatPos1);
 moveBoat(boatPos2);
}

void moveBoat(PVector boatPos) {
 //move boat and
 //update boat coordinates
 boatPos.x += 10;
 boatPos.y += 10;
}
Re: no parameter-passing?
Reply #6 - Mar 4th, 2009, 12:07am
 
Yep, I get it.
Note to future me: Read this:
http://processing.org/reference/class.html
Page Index Toggle Pages: 1