# Generalisation of function

**URL:** <https://discourse.processing.org/t/generalisation-of-function/27061>\
**Category:** Beginners\
**Created:** [January 14, 2021, 5:42pm UTC](https://discourse.processing.org/t/generalisation-of-function/27061 "2021-01-14T17:42:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafsing](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rafsing/32/10134_2.png) [@Rafsing](https://discourse.processing.org/u/Rafsing)\
**Post date:** [January 14, 2021, 5:42pm UTC](https://discourse.processing.org/t/generalisation-of-function/27061/1 "2021-01-14T17:42:20Z")

</div>

How can I generalize pendulumManagement() to take as arguments the names of the functions to fire and the boolean to decide on?

```auto
void pendulumManagement()
{
  if (pendul) {
    singlePendulumManagement();
  } else {
    dublePendulumManagement();
  }
}
```

---

<div class="post-metadata">

**Author:** ![micycle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micycle/32/201_2.png) [@micycle](https://discourse.processing.org/u/micycle)\
**Post date:** [January 14, 2021, 6:09pm UTC](https://discourse.processing.org/t/generalisation-of-function/27061/2 "2021-01-14T18:09:24Z")

</div>

Sounds like you want reflection.

> <https://stackoverflow.com/questions/160970/how-do-i-invoke-a-java-method-when-given-the-method-name-as-a-string>

---

<div class="post-metadata">

**Author:** ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)\
**Post date:** [January 14, 2021, 6:31pm UTC](https://discourse.processing.org/t/generalisation-of-function/27061/3 "2021-01-14T18:31:41Z")

</div>

In Java you would use a Class as argument and then call a class method depending on the boolean

---

<div class="post-metadata">

**Author:** ![Rafsing](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rafsing/32/10134_2.png) [@Rafsing](https://discourse.processing.org/u/Rafsing)\
**Post date:** [January 14, 2021, 6:32pm UTC](https://discourse.processing.org/t/generalisation-of-function/27061/4 "2021-01-14T18:32:22Z")

</div>

How can I do it in processing?

---

<div class="post-metadata">

**Author:** ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)\
**Post date:** [January 14, 2021, 6:47pm UTC](https://discourse.processing.org/t/generalisation-of-function/27061/5 "2021-01-14T18:47:52Z")

</div>

I think you need to do a bit of homework by yourself too.

You’d need a base class (pendulum) that has methods  
singlePendulumManagement() and dublePendulumManagement(). It can be an interface too. Then you’ll extend that base class (or interface) to classes that have different implementations of those functions.

Function call could be like this  
`void pendulumManagement(BaseClass bc, boolean pendul)`

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [January 14, 2021, 11:28pm UTC](https://discourse.processing.org/t/generalisation-of-function/27061/6 "2021-01-14T23:28:36Z")

</div>

Reflections should work but they’re not easy. I do however have some code I can share.

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [January 15, 2021, 12:25am UTC](https://discourse.processing.org/t/generalisation-of-function/27061/7 "2021-01-15T00:25:54Z")

</div>

> <https://stackoverflow.com/questions/63866964/passing-different-variables-into-a-reflection-getmethod-and-invoking-said-method>
