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.
Page Index Toggle Pages: 1
User functions? (Read 379 times)
User functions?
Jun 9th, 2009, 12:32pm
 
So, I just started a program and I created a helper function:

Code:
private void findSlopeIntercept {
 this.m = (y[2]-y[1])/x[2]-x[1]);
 this.b = y[1]-(m*x[1]);
}


When I try to give it a test run, I get an error at private. So now I'm not even sure if user created functions are legal in Processing.

I think I could outsource everything to an outside class, or failing that I could write everything out directly. But both ways seem kind of messy.

I'm new to Processing, and I haven't found anything on this. I figured the boards would be the quickest way to get an answer.
Re: User functions?
Reply #1 - Jun 9th, 2009, 12:51pm
 
Don't forget to add the parens after your function name:

private void findSlopeIntercept() {
 this.m = (y[2]-y[1])/x[2]-x[1]);
 this.b = y[1]-(m*x[1]);
}
Re: User functions?
Reply #2 - Jun 9th, 2009, 12:59pm
 
That was my slip.

I also had to remove private from the function. I guess Processing doesn't accept that.
Re: User functions?
Reply #3 - Jun 9th, 2009, 1:02pm
 
What happens when you get rid of "private"?  I've only ever seen private functions used within classes, never just in the main sketch.  (Not sure it will work.)
Re: User functions?
Reply #4 - Jun 9th, 2009, 1:07pm
 
Scott Murray wrote on Jun 9th, 2009, 1:02pm:
What happens when you get rid of "private"  I've only ever seen private functions used within classes, never just in the main sketch.  (Not sure it will work.)


Sorry, I edited my post as you responded.

It works now as:

Code:
void findSlopeIntercept() {
m = (y[2]-y[1])/(x[2]-x[1]);
b = y[1]-(m*x[1]);
}


I'm a bit wary with using all global variables, but it doesn't seem to work otherwise. I don't know if its Processing or if I'm just rusty with my Java.

There isn't really a need for private in this case anyways, it's just a habit of mine.
Page Index Toggle Pages: 1