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 & HelpPrograms › Passing a reference to a function
Page Index Toggle Pages: 1
Passing a reference to a function (Read 435 times)
Passing a reference to a function
Jul 7th, 2006, 4:59am
 
Is it possible to pass a reference to a function? In JavaScript, the code would go like this, but I have no clue in Java/Processing.
Code:

// Note: this is JavaScript, not Java/Processing
function referenced() {
alert("Hello.");
}
function dostuff(func) {
func();
}
dostuff(referenced);
// This code would make an alert box with the text "Hello."


Is this possible to do in Java/Processing? If so, how?
Re: Passing a reference to a function
Reply #1 - Jul 7th, 2006, 5:32am
 
Oh, I figured it out on my own. For anyone looking to do the same thing:
Code:

class Blah
{
public Blah() {}
public void referenced()
{
println("Hello.");
}
}
void dostuff(Blah func)
{
func.referenced();
}
void setup()
{
dostuff(new Blah());
}


It's a bit annoying, but it does the trick.
Bleh Tongue
Page Index Toggle Pages: 1