Really embarrassing questions!

setup(), draw() and ellipse(), for example, are all built-in functions with void return.

  1. Why do I need to write “void” before setup() and draw(), but not before ellipse()?
  2. Not writing “void” before setup() and draw() results in an error. OK. But writing “void” before ellipse() results in an error too. Why?

Thanks in advance.

2 Likes
  1. B/c we’re defining methods which don’t return anything; thus void.
    Method PApplet::ellipse() is already defined, so it’s ready to be invoked.
  2. Idem above.

Not stupid questions at all!

You wrote:

setup(), draw() and ellipse(), for example, are all built-in functions

I wouldn’t say so.

ellipse() is a built-in function.

But setup(), draw() and mousePressed() (there are many more) are not built-in functions but we have to define the functions. But they are known to processing so they get called automatically when we have defined them. But it’s a difference between built-in function and known-to-processing functions (not sure what the correct term is though; technically they are ‘callback’ functions).

To define setup(), draw() and mousePressed() we have to declare their return type which is void (but can also be String or so).

Definition of a function:

void setup() {
  size (1400, 800);
}

To call a function we just state its name ellipse(…); or say something like int a = add(…);

Summary

  • We have to define setup(), the function doesn’t already exist, it’s not a built-in function.

  • We don’t define ellipse(), the function does already exist, it’s a built-in function.

  • Distinguish between defining a function and calling a function.

void is the return type of a function that doesn’t return anything. E.g. setup().

Remark

You wrote:

with void return

Better say: with the return type ‘void’.

You wrote:

  1. Why do I need to write “void” before setup() and draw(), but not before ellipse()?

This is a misunderstanding:

  • when we write void setup() {...} we define the function. It gets called automatically.

  • when we write ellipse() we don’t define the function. We just call it. We use it. Big difference.

  • In the example below we define the function “add()” and also call it.

You wrote:

  1. Not writing “void” before setup() and draw() results in an error. OK. But writing “void” before ellipse() results in an error too. Why?

Again, similarly, when writing “void” before setup() and draw() we define the function. Here the usage of void (or any other return type) is mandatory. See "int add()" in the example below where we define the function add().

When writing ellipse(....) we don’t define the function but call it. When calling it, a return type mustn’t be stated.

What is a return type?

What is a return type? When declaring a variable we tell it which type it has: int, float, String etc.; same with what a function returns.

When we use the function to assign its return value to a variable, the return type and the type of the variable must be the same. See example below.

Here is an example:

We define the function int add() that returns int (and not void, which means nothing).

But we have to call add() in draw(), otherwise it’s not used. Not the same thing.

int a=0; 

void setup() {
  size (1400, 800, P3D);
}

void draw() {
  a = add( 3, 4 );  // using the function add 
  println(a);
  noLoop();
}

// defining the function add !!!!!!!!!!!!!!!!!!!!!!
int add (int n1, int n2) {
  // it returns an int value
  return n1+n2;
}

Further Reading

So there are functions which are

  • in-built
  • not in-built but known by processing
  • functions that receive parameters (ellipse()) and others that don’t
  • functions that return no value (void) and others that return a result (like add() above)

see programming and functions · Kango/Processing-snippets Wiki · GitHub

Chrisir

4 Likes

Hey There!

There isn’t such a thing as an embrassing question or stupid! I guess we are made to feel by som like they are but it’s always the right thing to ask a question. Everyone is at a beginning, like your a beginner with processing. Some are just advanced in other things and beginners in others. But no matter how good you are at something at one point of time you were a beginner. I know I was with Processing ! And coding ! Everyone here was a beginner at some point of time. Now you are here, and don’t let that intimidate you. We are here to help ! All questions are as perfect as they are !

5 Likes

Thanks for such a comprehensive reply.

1 Like