Function expressions

edited July 2016 in JavaScript Mode

I've been reading about function expressions in Javascript. Like var greet = function greet(name, timeOfDay) { return “Good “ + timeOfDay + “ “ + name + “!”; }

Is it possible to do something like this in Processing? The way I am inclined to try this is setting the function to float and creating a variable to reference it in the body of my program. Just curious if anyone dealt with this much.



  • Processing got many flavors. And Java Mode is the main 1.
    In Java we've gotta declare which type a function returns. Plus all the types of its parameters.

    String greet(String name, String timeOfDay) {
      return "Good " + timeOfDay + " " + name + "!";
  • Thanks for reminding me one can switch modes! I was curious if you could have an expression like float x = String greet(String name, String timeOfDay) { return "Good " + timeOfDay + " " + name + "!"; }

  • edited July 2016

    In Java, functions (also classes/interfaces) got their own namespace.
    It means we can have a variable called greet at the same time we have a function called greet() too!
    As you may be guessing by now, that's gonna fail miserably in JS! :-SS
    JS stores references for anything in variables and properties, including functions. :-B

  • edited July 2016

    A tricky sample so you can understand that in JS functions share the same namespace as everything else: :-\" Hit F12 to open your browser's console and paste those 3 statements below: >-)

    var greet = 10;
    function greet() { return 'Hi!'; }
    console.log( greet() ); // CRASH!!!

    You're gonna get: Uncaught TypeError: greet is not a function(…).
    It is instead the number 10! :P

  • edited July 2016

    In contrast just check it out this confusing sketch using String everywhere: :ar!

    void setup() {
      String String = "String";
    String String(String String) {
      return String + " is everywhere!";

    It's mind-twisting but Java is smart enough to distinguish which "String"s are the variable, the parameter, the function and the class datatype! \m/

  • That is cool! Thanks for the info. Getting a better sense for JS and java differences

Sign In or Register to comment.