Where to declare functions

A newbie (to processing) question: I tried out processing with a few simple sequential lines, e.g. size(), background(), ellipse(). Fine. Added a vector and a loop to print it. OK. Now I declared a function and called it from within the loop. Error! After some experimenting, it looks like I have to have a setup() block and a draw() block if I use my own functions. Correct?

I guess it has to do with a wrapper that processing adds to my few lines when setup and draw are not around, and I can't declare functions within that wrapper?

Ove

Answers

  • Answer ✓

    it looks like I have to have a setup() block and a draw() block if I use my own functions. Correct?

    Yes

    This explains setup & draw methods

  • edited March 2015 Answer ✓

    What is programming?

    Programming is explaining to a computer how to solve a task. Imagine you go on holiday and a friend has to take care of your home and other tasks. You have to explain him what to do. He is very precise but a little dumb. So you have to explain it step by step.

    Static sketches and Interactive sketches

    static sketches are static, meaning they can't show a moving ball or so. They just run once.

    This is a typical static sketch:

    size(600,600);
    rect (200,200,30,40);
    

    You can't have functions in static mode.

    This is a typical Interactive sketch:

    void setup() {
       size(400, 400);
       stroke(255);
       background(192, 64, 0);
     }
    
     void draw() {
       line(150, 25, mouseX, mouseY);
     }
    

    quote:

    A program written as a list of statements (like the previous examples) is called a static sketch. In a static sketch, a series of functions are used to perform tasks or create a single image without any animation or interaction. Interactive programs are drawn as a series of frames, which you can create by adding functions titled setup() and draw() as shown in the code below. These are built-in functions that are called automatically.

    This quote stems from a tutorial:

    https://www.processing.org/tutorials/overview/

    • In Interactive sketches you can write your own functions (only in interactive sketches).

      • setup and draw are Functions. They are built-in functions that are called automatically.

      • Nearly all sketches are Interactive sketches.

      • You need setup() to write your own functions.

      • We can't have functions inside another function in Java.

      • setup() runs once, draw() runs on and on.

    What is a function?

    In math a function receives some values (x), works with them and returns a value (y):

    f(x) = 3*x + 4;

    f(3) = 13;

    In programming, a function is a block of code. You can think of a function as defining a new command for the computer to learn.

    Again, it receives some values (the parameters), works with them and returns a value (the result or return value). So it is like a small factory.

    Let's look at the functions we already know:

    void setup() {
      size(400, 400);
      stroke(255);
      background(192, 64, 0);
    }
    

    The function setup has

    • a name: setup

    • it doesn't receive any parameters. Therefore the brackets ( and ) are empty.

    • it doesn't return anything, therefore it says void before it. It returns nothing (empty, void).

    • its text is within { and }

    Your own functions always follow the same structure.

    Also line() is a function. It receives 4 parameters inside ( and ) and returns nothing (void). It just does something, drawing the line according to its parameters. Hence the parameters are very important.

    Also draw() is a function.

    A function that just does something

    A function that just does something without getting a parameter or without returning something is rather dumb. Nevertheless it's still recommend since it can structure what happens in draw(): With functions you have defined blocks of code that get called from draw() instead of having endless lines within draw(). The function draw() gets easier to read.

    The function rightAngle() is declared after draw() and is called in draw().

    A function never gets called automatically (apart from setup(), draw() and other in-build functions of processing).

    void setup() {
      size(600, 500);
      noFill();
      stroke(255, 200, 120);
      strokeWeight(3);
    }
    
    void draw() {
      background(0);
      rightAngle();
    }
    
    void rightAngle() {
      line (100, 100, 200, 100);
      line (200, 100, 200, 200);
    }
    

    A function with parameters

    A function with parameters is more useful:

    void setup() {
      size(600, 500);
      noFill();
      stroke(255, 200, 120);
      strokeWeight(3);
    }
    
    void draw() {
      background(0);
      rightAngle(260, 180);
    }
    
    void rightAngle(float startX, float startY) {
      line (startX, startY, startX+100, startY);
      line (startX+100, startY, startX+100, startY+100);
    }
    

    A function that really returns a result

    Now let's look at a function that really returns something.

    void setup()
    {
      // init (runs only once)
      size(800, 600);
    } // func
    
    void draw()
    {
      // runs on and on
      background(255);
    
      int a1;
      // get a value from the function
      a1 = add( 3, 6);
      // show the value
      fill(0);
      text(a1, 100, 100);
    } // func
    
    int add(int toAdd1, int toAdd2) {
      int buffer;
      // we evaluate a result
      buffer =  toAdd1 + toAdd2;
      // we return the result
      return buffer;
    } // func
    

    add() is the function that really returns something. It can receive 2 parameters which are added together. The result is returned.

    Instead of void, the line of the function starts with int since it returns a value of the type int.

    The function add() isn't called automatically (like setup and draw) but we have to call it ourselves in draw(). When we call it, we need to pass it 2 parameters (3 and 6) and let draw() receive a result which we store in the variable a1 (which is of type int like the return type of the function).

    Please read this sketch carefully. You must understand everything in it. If not come back and ask. Copy the sketch into processing and play with it. Make a new sketch with a function minus() and test it.

    We have to distinguish where we define the function and where we call the function.

    The call of the function must match the definition of the function:

    • Number of parameters (2)

    • Type of parameters (two times int)

    • the type of the return value must match the type of a1.

    We now taught processing a new command: add().

    ;-)

  • You answered my question with Yes, I have to have setup() and draw() if I use functions. Actually, after some experimenting, I saw that only setup() is needed (it works like main in pure Java, C# and others).

    But nowhere in your examples did it say that I have to have setup(). And nowhere else either that I could find. How to use functions was not my problem.

    But thanks a lot! Moving along... like it so far!

  • edited March 2015 Answer ✓
    • @ofred, main reason why we can't implement our own functions in "static mode" is b/c Processing's pre-processor wraps everything up inside setup() behind the curtain. :-B
    • However, we can't have functions inside another function in Java. And that's why it fails! #-o
    • Plus we can't instantiate our own classes before the line they're defined in "static mode" either!
    • We can check that out by hitting CTRL+E in the PDE and look at the generated ".java" real code! *-:)
  • Just for amusement, my hack against "Static Mode"! >:)

    class MyFunction {
      MyFunction(String... s) {
        println(s);
      }
    }
    
    new MyFunction("Cheating on", "Processing's", "\"Static Mode\"!", ";-)");
    exit();
    
  • Indeed, draw() is necessary only if you need animation / drawing on each frame. And / or to get events (key / mouse) to work.

Sign In or Register to comment.