# Terminology: function, method and event?

**URL:** https://discourse.processing.org/t/terminology-function-method-and-event/16855
**Category:** Beginners
**Created:** [January 4, 2020, 11:31am UTC](https://discourse.processing.org/t/terminology-function-method-and-event/16855 "2020-01-04T11:31:20Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Teljemo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/teljemo/32/6486_2.png) [@Teljemo](https://discourse.processing.org/u/Teljemo)
#### Post date: [January 4, 2020, 11:31am UTC](https://discourse.processing.org/t/terminology-function-method-and-event/16855/1 "2020-01-04T11:31:20Z")

</div>

Hi  
I’m confused and the more I google the more confused I get, so I better ask here 🙂  
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. 😛

**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? 😮

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?** 🙂

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 4, 2020, 12:41pm UTC](https://discourse.processing.org/t/terminology-function-method-and-event/16855/2 "2020-01-04T12:41:15Z")

</div>

I wrote an article about this, see below.

**Definition of a function**

This is a function:

```auto
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](https://github.com/Kango/Processing-snippets/wiki/programming-and-functions)

Chrisir

---

<div class="post-metadata">

### Author: ![Teljemo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/teljemo/32/6486_2.png) [@Teljemo](https://discourse.processing.org/u/Teljemo)
#### Post date: [January 4, 2020, 6:52pm UTC](https://discourse.processing.org/t/terminology-function-method-and-event/16855/3 "2020-01-04T18:52:53Z")

</div>

Thx 🙂  
This cleared things up!

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [January 4, 2020, 6:54pm UTC](https://discourse.processing.org/t/terminology-function-method-and-event/16855/4 "2020-01-04T18:54:03Z")

</div>

> [@Teljemo](#):
>
> Is a function only another word for method when you call it from a `class` like _buss_.**moveThatBuss()**?

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):

> **[Method (computer programming)](https://en.wikipedia.org/wiki/Method_(computer_programming))**
>
> A method in object-oriented programming (OOP) is a procedure associated with an object, and generally also a message. An object consists of state data and behavior; these compose an interface, which specifies how the object may be used. A method is a behavior of an object parametrized by a user.
> Data is represented as properties of the object, and behaviors are represented as methods. For example, a Window object could have methods such as open and close, while its state (whether it is open or c...

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:

> **[Function (computer programming)](https://en.wikipedia.org/wiki/Subroutine)**
>
> In computer programming, a function or subroutine is a sequence of program instructions that performs a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed.
> Functions may be defined within programs, or separately in libraries that can be used by many programs. In different programming languages, a function may be called a routine, subprogram, subroutine, or procedure; in object-oriented programming (OOP), it may be called...

> [@Teljemo](#):
>
> Is a method everything you create starting with `void` like: `void moveThatBuss() {}`?

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

- [void / Reference / Processing.org](http://Processing.org/reference/void.html)
- [return / Reference / Processing.org](http://Processing.org/reference/return.html)

> [@Teljemo](#):
>
> In what category does for example **fill()** and **rect()** fit in?

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

> [@Teljemo](#):
>
> Where does **setup()** and **draw()** fit in?

Both are user-customized callback methods from `class` PApplet:  
[**setup()**](https://processing.org/reference/setup_.html), along w/ [**settings()**](https://processing.org/reference/settings_.html), are invoked once at the beginning by our sketch’s “Animation” [Thread](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Thread.html).

While [**draw()**](https://processing.org/reference/draw_.html) are invoked/called-back about 60 FPS rate by default.

> [@Teljemo](#):
>
> Is event already existing `void`’s like, `void mousePressed() {}`

[**mousePressed()**](http://processing.github.io/processing-javadocs/core/processing/core/PApplet.html#mousePressed--) is another PApplet method callback, but it’s triggered by a mouse event:

> **[mousePressed() / Reference](https://processing.org/reference/mousePressed_.html)**
>
> The mousePressed() function is called once after every time a mouse button is pressed. The mouseButton variable (see the related reference entry) can be used to determine which button ha…

> [@Teljemo](#):
>
> And `if else` , is this a function method or event?

Those 2 are Java keywords, used for conditional branching:

> **[Reserved word | Definition](https://en.wikipedia.org/wiki/Reserved_word#Definition)**
>
> Some use the terms "keyword" and "reserved word" interchangeably, while others distinguish usage, say by using "keyword" to mean a word that is special only in certain contexts but "reserved word" to mean a special word that cannot be used as a user-defined name. The meaning of keywords, and the meaning of the notion of keyword, differs widely from language to language. Concretely, in ALGOL 68, keywords are stropped (in the strict language, written in bold) and are not reserved words – the unstro...

> **[Branch (computer science)](https://en.wikipedia.org/wiki/Branch_(computer_science))**
>
> A branch is an instruction in a computer program that can cause a computer to begin executing a different instruction sequence and thus deviate from its default behavior of executing instructions in order.\[a\] Branch (or branching, branched) may also refer to the act of switching execution to a different instruction sequence as a result of executing a branch instruction. Branch instructions are used to implement control flow in program loops and conditionals (i.e., executing a particular sequence...

> [@Teljemo](#):
>
> What about _loops_?

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

> **[Control flow | Loops](https://en.wikipedia.org/wiki/Control_flow#Loops)**
>
> A loop is a sequence of statements which is specified once but which may be carried out several times in succession.
> The code "inside" the loop (the body of the loop, shown below as xxx) is obeyed a specified number of times, or once for each of a collection of items, or until some condition is met, or indefinitely.
> In functional programming languages, such as Haskell and Scheme, both recursive and iterative processes are expressed with tail recursive procedures instead of looping constructs th...

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [January 6, 2020, 7:30pm UTC](https://discourse.processing.org/t/terminology-function-method-and-event/16855/5 "2020-01-06T19:30:36Z")

</div>

@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](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.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [January 6, 2020, 8:37pm UTC](https://discourse.processing.org/t/terminology-function-method-and-event/16855/6 "2020-01-06T20:37:19Z")

</div>

@jeremydouglass

maybe we can make a tutorial from this?

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

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [January 6, 2020, 8:44pm UTC](https://discourse.processing.org/t/terminology-function-method-and-event/16855/7 "2020-01-06T20:44:33Z")

</div>

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:

> **[Calling Functions](https://happycoding.io/tutorials/processing/calling-functions)**
>
> Learn the first step to coding: calling functions!
