# Individualize functions in P5.js

**URL:** <https://discourse.processing.org/t/individualize-functions-in-p5-js/32051>\
**Category:** Coding Questions\
**Created:** [September 1, 2021, 3:27pm UTC](https://discourse.processing.org/t/individualize-functions-in-p5-js/32051 "2021-09-01T15:27:42Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)\
**Post date:** [September 1, 2021, 3:40pm UTC](https://discourse.processing.org/t/individualize-functions-in-p5-js/32051/2 "2021-09-01T15:40:20Z")

</div>

Hi @Kevincroos,

First of all one thing about your code:

You shouldn’t put the `drawBall()` function (and others) inside the `draw()` function because it’s not optimized and it’s not needed in the current configuration since the function will always be the same. As stated in the following website:

> Unlike other types of values, functions typically don’t change; a function is created to perform a specific task. So it doesn’t make much sense to waste CPU cycles recreating a somewhat static value over and over again.

> **[Stop Nesting Functions! (But Not All of Them) | Envato Tuts+](https://code.tutsplus.com/stop-nesting-functions-but-not-all-of-them--net-22315t)**
>
> JavaScript is over fifteen years old; nevertheless, the language is still misunderstood by what is perhaps the majority of developers and designers using the language. One of the most powerful,...

> [@Kevincroos](#):
>
> But now, i need individualize those balls, i have an random variable to display the ball in random locations, but calling two or more balls then do not appears in diferents locations, its the same place of the first ball

Aah! this is exactly the purpose of [Object-oriented programming](https://en.wikipedia.org/wiki/Object-oriented_programming): being able to represent your data as units of data (called objects).

Check a previous thread I did on the subject (with balls but in Java, it shouldn’t be too hard to do it in JS):

> [@Problems with my mini game](https://discourse.processing.org/t/problems-with-my-mini-game/21949/2):
>
> Hi, Welcome to the community! slight_smile Before trying to solve your problem, you are facing a well known problem where the structure of your code is actually describing [object oriented programming](https://en.wikipedia.org/wiki/Object-oriented_programming) : So object oriented programming is a way to structure your code by using objects to model the data that your are manipulating. In this special case, your are creating Balls, and a Ball is described as : a position (x and y coordinates on the screen) a speed (described as a vector, xDir and y…

Also see MDN’s documentation on the subject:

> **[Classes in JavaScript - Learn web development | MDN](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Classes_in_JavaScript)**
>
> In this article, we've gone through the main tools available in JavaScript for writing object-oriented programs. We haven't covered everything here, but this should be enough to get you started. Our article on Classes is a good place to learn more.

---

_[View the full topic](https://discourse.processing.org/t/individualize-functions-in-p5-js/32051)._
