We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
Yes
This explains setup & draw methods
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:
You can't have functions in static mode.
This is a typical Interactive sketch:
quote:
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:
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).
A function with parameters
A function with parameters is more useful:
A function that really returns a result
Now let's look at a function that really returns something.
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!
Just for amusement, my hack against "Static Mode"! >:)
Indeed, draw() is necessary only if you need animation / drawing on each frame. And / or to get events (key / mouse) to work.