What you are looking for, functions that are variables, are called lambda expressions. The problem is that they were introduced only in Java 8, and, as far as I know, Processing uses older versions of Java, so you are out of luck with this idea.
Maybe there’s some hacky cool way to implement it - maybe you could just make up new classes for every function, and then juggle them around in a weird way to do hacky things, and each one of these classes has a f(float a, float b) type function, but I can’t think of such, at least not right now, and I don’t even know if it’s possible even that way.
Edit: here’s even complete real code of your example:
If you paste it into Processing, however, it would show no errors, except a message talking about Java 8 pointing to the definition of myFunction, which is this: Lambda expressions are only allowed at source level 1.8 or above
It’s not really hacky. Anonymous classes were added in Java 1.1 precisely to support a form of functional programming in exactly this way.
MyCoolInterface myFunction = new MyCoolInterface() {
public float operation(float a,float b) {
return a * b;
}
};
Or you can just use that as a method argument rather than assigning to a variable. All a bit clunky though. Lambdas were added as a cleaner and better performing alternative.
I’ve wet my feet a little on the Java package java.util.function by instantiating a [u]Predicate[/u] and passing it as an argument at these 2 old sketch links below: