Terminology: function, method and event?

Hi
I’m confused and the more I google the more confused I get, so I better ask here :slight_smile:
As me and my son study Processing im now trying to document our progress and I don’t know If i’m suppose to write that we are looking at a function, method or event most of the time. :stuck_out_tongue:

This is what I guess:
Is a function only another word for method when you call it from a class like, buss.moveThatBuss()
Is a method everything you create starting with “void” like, void moveThatBuss()
Is event already existing “void’s” like, void mousePressed()

In what category does for example “fill()” and “rect()” fit in?
Where does “setup()” and “draw()” fit in?
And “if else”, is this a function method or event?
What about loops?..
As you can see… I’m confused about how to categorize stuff. What more categories is there? :open_mouth:

I’ve read multiple articles, guides, forum posts and also of course looked at Shiffman on Youtube and his book “learning Processing 2nd edition”.
People are using the terms differently, what is correct?

Can you guys help me sort this out? :slight_smile:

2 Likes

I wrote an article about this, see below.

Definition of a function

This is a function:

void drawLineToMouse() {
  line(150, 25, mouseX, mouseY);
} // end of function 
  • A distinct section of code. Here the function drawLineToMouse() is defined.
  • It has a name and can be seen as a new command that you define.
  • You have to call it from draw() (for example): drawLineToMouse();. Here the function drawLineToMouse() is called.
  • The part between the curly brackets { and } is the body of the function where its code is.
  • Basically, there is no code allowed outside of functions.
  • If, else, for-loops and the other loops, fill and rect and line I would see as commands, not as functions.

Details

But Yeah, there are :

  • build-in functions like line(), fill(), rect() [whose code you can’t read but that you can use]
  • build-in functions like setup() and draw(), keyPressed(), mousePressed() [which you can write yourself and are then called automatically]
  • you can write your own functions like train() that you need to call from draw(), otherwise they don’t run. Not called automatically.
  • event functions: mousePressed() etc. are event functions and are called automatically
  • if-else and loops are all commands, that you can use within a function
  • in a class, functions are called methods
  • void draw: void is the return type, in this case the function (or method) isn’t returning anything [void]. Other return types are boolean, int, String, an array or a class…!

see https://github.com/Kango/Processing-snippets/wiki/programming-and-functions

Chrisir

3 Likes

Thx :slight_smile:
This cleared things up!

1 Like

It’s the opposite! Method is a function/procedure associated w/ a class and its objects/instances which performs a task over its data (fields/properties):


That is, all methods are functions, but not all functions are methods.

Notice though that the “correct” terminology varies depending on the programming language we’re currently using:

In Java, the keyword void is used for declaring that a method/function doesn’t return anything:

They’re methods from Processing’s class PApplet which acts upon its rendering main canvas (field PGraphics g) state:
Processing.GitHub.io/processing-javadocs/core/processing/core/PApplet.html

Both are user-customized callback methods from class PApplet:
setup(), along w/ settings(), are invoked once at the beginning by our sketch’s “Animation” Thread.

While draw() are invoked/called-back about 60 FPS rate by default.

mousePressed() is another PApplet method callback, but it’s triggered by a mouse event:
Processing.org/reference/mousePressed_.html

Those 2 are Java keywords, used for conditional branching:


Java got 3 keywords for loops: for, while & do:

2 Likes

@Chrisir gives a great summary, and @GoToLoop provides good context.

One additional wrinkle:

The Processing (default Java mode) documentation describes programming with functions, some of which are built in, and some of which may be called automatically in various ways by a running sketch (like settings(), setup(), draw(), and the event handlers). All these top-level entities are called “functions” in Processing documentation – and the ones you write yourself are also functions,

Some of these Processing functions are also methods – that is, they belong to built-in classes within Processing. Examples are IntList.append(), PVector.lerp(), Table.addRow() – these are functions, but functions attached to a specific class or instance object: methods. When you create a String object in Processing and call .equals(), that is a method – it belongs to the String.

The wrinkle is that Processing is compiled under-the-hood into Java. Almost everything that Processing presents as a top-level a “function” ultimately in Java becomes a method of a PApplet object – your sketch. So, at run-time, everything is only methods on objects–mainly methods on one object, PApplet. If you click from any Processing reference page over to the JavaDoc and look up PApplet, you will see that a large portion of the Processing language functions are actually a set of methods on that one object.

http://processing.github.io/processing-javadocs/core/processing/core/PApplet.html

This is helpful to know when communicating with programmers coming from (or heading into) the Java world. In fact, in Java documentation everything is always described as a “method” (all subroutine functionality is always connected to some class or object) and never a “function” – formally you don’t use the word “function” at all. This is good to know when you encounter people who are primarily Java programmers, for example on StackOverflow – when they say “method” (like “the draw() method”) they are referring to what a Processing tutorial will refer to as a “function” — anything anywhere in your sketch that takes an argument and returns a value.

3 Likes

@jeremydouglass

maybe we can make a tutorial from this?

I think, there is no tutorial about functions and methods?

2 Likes

Nice idea. Other than the fact that very introductory Processing demonstrates function use and uses the word “function”, the thing I can think of that already exists is this:

3 Likes