What is the purpose of void setup() ?

edited January 2018 in Hello Processing

I am watching the Codetrain videos in order to learn how to program using Processing. In video 6.3

https://youtube.com/watch?v=h4ApLHe8tbk&index=3&list=PLRqwX-V7Uu6bm-3M4Wntd4yYZGKwiKfrQ

while discussing loops, Daniel creates a sketch that uses the functions size, stroke, background and strokeWeight. In all previous videos these functions were inside of setup but in this video they stand alone at the beginning of the code.

Is there any difference in the way these functions act when inside or outside of setup? If not, what is the purpose of the setup function?

Answers

    • setup() & draw() are the major callback functions in Processing.
    • When Processing starts, and all basic things are already initialized, it call backs our setup() function once.
    • Then it keeps calling back draw() function at about 60 FPS by default.
  • I think I knew this, although I am not certain what a callback function is. But that does not address my question about why Daniel intialized size, stroke, strokeWeight and background outside of setup

  • edited January 2018 Answer ✓

    Ah! You've meant a sketch w/o functions. That is called "immediate mode" or something like that. :\">

    The PDE (Processing's IDE) automatically wraps up the functionless sketch into 1 function behind the scenes. \m/

    That simplified style is for when a sketch got no animations.
    It simply renders 1 static image and it's done. :)>-

    Here's an online example using such style: :bz
    http://Studio.ProcessingTogether.com/sp/pad/export/ro.96P5CDnO6mAXt

  • ... although I am not certain what a callback function is.

    A callback function is a customized code which is expected to be invoked when some event happens.

    In the case of setup(), it is called back when the sketch is finished initializing itself.

    And draw() is called back each time the sketch finishes rendering the current canvas' content to the window.

  • So does this mean that Processing creates void setup automatically if there is none there?

  • edited January 2018
    • Something like that! PDE's preprocessor does lotsa of things behind the scenes. :-j
    • Its ultimate goal is to transpile all the ".pde" tabs into 1 valid ".java" file, so it can be compiled & run under Java. ~O)
    • Just 1 lil' thing: Avoid using the keyword void when referring to a function. L-)
    • Keyword void simply means that a function doesn't return anything: :\">
      https://Processing.org/reference/void.html
    • When talking about functions, parentheses () is the way to indicate that something is a function. :-B
  • edited January 2018

    Quote:

    This is a static sketch without functions (and therefore without setup()):

      size(400, 400);
      background(192, 64, 0);
      stroke(255);
      line(150, 25, 270, 350);
    

    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.

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

    The setup() block runs once, and the draw() block runs repeatedly. As such, setup() can be used for any initialization; in this case, setting the screen size, making the background orange, and setting the stroke color to white. The draw() block is used to handle animation. The size() function must always be the first line inside setup().

    See

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

    Your question

    You wrote:

    So does this mean that Processing creates void setup automatically if there is none there?

    So, in both cases setup is not created automatically if there is none there.

    But of course, processing is starting things up under the hood before setup() is called (when it's there). That's why gotoloop wrote:

    In the case of setup(), it is called back when the sketch is finished initializing itself.

    Remarks

    • in case of a static sketch, there can't be a setup(), since there are no functions

    • in case of a Interactive program or sketch, I strongly recommend to always have a setup() (and size() as its first line)

    • Don't mix static and interactive approach. Either no command is in an function (static sketch) or all commands are in functions (interactive program)

  • This is helpful but it creates a new question for me. You say

    Either no command is in a function (static sketch) or all commands are in functions (interactive program)

    what is the definition of the word "command" ?

    In the videos that I have been watching, global variables are declared outside of setup(). Are variable declarations not considered a command?

  • Answer ✓

    Are variable declarations not considered a command?

    correct

    commands are fill, stroke, line() and rect() and all of those

  • Thanks to both of you who took the time to help me out

  • edited January 2018

    Are variable declarations not considered a command?

    Commands are fill(), stroke(), line() and rect() and all of those.

Sign In or Register to comment.