# Functions that don't have to be called

**URL:** https://discourse.processing.org/t/functions-that-dont-have-to-be-called/7829
**Category:** Beginners
**Created:** [January 25, 2019, 4:55am UTC](https://discourse.processing.org/t/functions-that-dont-have-to-be-called/7829 "2019-01-25T04:55:44Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![germaine](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/germaine/32/2103_2.png) [@germaine](https://discourse.processing.org/u/germaine)
#### Post date: [January 25, 2019, 4:55am UTC](https://discourse.processing.org/t/functions-that-dont-have-to-be-called/7829/1 "2019-01-25T04:55:44Z")

</div>

I’m used to writing functions and then calling them in draw() or setup(), but in other people’s sketches, I’ve found functions that clearly run but don’t seem to be called or referenced anywhere else in the code. Am I just misunderstanding something? Thanks.

---

<div class="post-metadata">

### Author: ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)
#### Post date: [January 25, 2019, 6:45am UTC](https://discourse.processing.org/t/functions-that-dont-have-to-be-called/7829/2 "2019-01-25T06:45:52Z")

</div>

Can you give a specific example of what you’re talking about?

Are you talking about functions like `mousePressed()` or `keyPressed()`? If so, Processing calls those automatically, just like it automatically calls the `setup()` and `draw()` functions.

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [January 27, 2019, 3:03am UTC](https://discourse.processing.org/t/functions-that-dont-have-to-be-called/7829/3 "2019-01-27T03:03:33Z")

</div>

i think that might be a example:  
@quark [Self rendering objects?](https://discourse.processing.org/t/self-rendering-objects/4427/9)

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [January 27, 2019, 11:34am UTC](https://discourse.processing.org/t/functions-that-dont-have-to-be-called/7829/4 "2019-01-27T11:34:25Z")

</div>

The “Self rendering objects” link shows how you can add graphic stuff to the frame without having to add code in the `draw()` method. The technique is aimed at library creators but can be used by sketchers. It should only be employed when there is a clear meaningful need to do so and not just on a whim.

There are other methods you can register but the sketcher needs to be able to create classes to get the best from this technique.

This [page](https://github.com/processing/processing/wiki/Library-Basics) tells you more about it.

---

<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 28, 2019, 4:14am UTC](https://discourse.processing.org/t/functions-that-dont-have-to-be-called/7829/5 "2019-01-28T04:14:24Z")

</div>

> [@Kevin](#):
>
> Are you talking about functions like `mousePressed()` or `keyPressed()` ? If so, Processing calls those automatically, just like it automatically calls the `setup()` and `draw()` functions.

Expanding on Kevin’s answer:  
The main methods (‘functions’) that Processing calls automatically in a sketch are:

1. **`setup()`** at the beginning, then whenever frameCount = 0
2. **`draw()`** repeatedly whenever `loop()` is set (on by default) or manually when `redraw()` is called
3. **`settings()`**, primarily if you need to set `size()` with a variable
4. **`exit()`** if you choose to override it – just be sure to call `super.exit()` at the end.
5. the interactive methods that check for input events:
  - keyPressed()
  - keyReleased()
  - keyTyped()
  - mouseClicked()
  - mouseDragged()
  - mouseMoved()
  - mousePressed()
  - mouseReleased()
  - mouseWheel()

Some libraries also what can see like magic functions – generally the hooks / callbacks work through `registerMethod()`. Like `keyPressed()`, you put code in those functions and they just run, even though nothing in the sketch explicitly calls them. For example, in the Video library, `movieEvent()` is automatically called whenever a new frame is available; the sketch author can just read the frame, or they can put whatever code they want there.

```auto
// Called every time a new frame is available to read
void movieEvent(Movie m) {
  m.read();
}

```
