# Void function is returning values but shouldn't

**URL:** <https://discourse.processing.org/t/void-function-is-returning-values-but-shouldnt/26528>\
**Category:** Beginners\
**Created:** [December 27, 2020, 12:18am UTC](https://discourse.processing.org/t/void-function-is-returning-values-but-shouldnt/26528 "2020-12-27T00:18:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Divitiacus](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@Divitiacus](https://discourse.processing.org/u/Divitiacus)\
**Post date:** [December 27, 2020, 12:18am UTC](https://discourse.processing.org/t/void-function-is-returning-values-but-shouldnt/26528/1 "2020-12-27T00:18:57Z")

</div>

Hi there,

as far as I know, void functions do not return values. However, when I run the code below,  
the wind vector gets smaller and smaller with each frame, because the va value is returned back as new wind vector.  
Why is that?  
I would expect that wind stays the same all the time and just serves as input for the function.

```auto
PVector wind;

void setup(){
  wind = new PVector(10,0);
}

void draw (){
  println(wind);
  apply(wind);
}

void apply(PVector wind_){
  PVector va = wind_.sub(1,0); 
  
}

```

---

<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:** [December 27, 2020, 12:25am UTC](https://discourse.processing.org/t/void-function-is-returning-values-but-shouldnt/26528/2 "2020-12-27T00:25:44Z")

</div>

> [@Divitiacus](#):
>
> `PVector va = wind_.sub(1,0); `

- PVector::[**sub()**](https://processing.org/reference/PVector_sub_.html) is a mutating method.
- Objects live in the heap memory.
- Variables are merely a reference to that heap memory block.
- BtW, field _wind_, parameter _wind\__ and variable _va_ are all aliases to the same heap memory block.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [December 27, 2020, 1:09am UTC](https://discourse.processing.org/t/void-function-is-returning-values-but-shouldnt/26528/3 "2020-12-27T01:09:58Z")

</div>

Hello,

Some references:

- [https://processing.org/tutorials/pvector/](https://processing.org/tutorials/pvector/)
- [https://natureofcode.com/book/chapter-1-vectors/](https://natureofcode.com/book/chapter-1-vectors/)
- [https://processing.org/reference/PVector.html](https://processing.org/reference/PVector.html)

`:)`

---

<div class="post-metadata">

**Author:** ![Divitiacus](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@Divitiacus](https://discourse.processing.org/u/Divitiacus)\
**Post date:** [December 27, 2020, 2:12am UTC](https://discourse.processing.org/t/void-function-is-returning-values-but-shouldnt/26528/4 "2020-12-27T02:12:00Z")

</div>

Thanks.

I am actually working with Code of Nature right now but must have overlooked the section 2.4., where he explains that this would happen and how to avoid it.

---

<div class="post-metadata">

**Author:** ![jay\_m](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jay_m/32/11020_2.png) [@jay\_m](https://discourse.processing.org/u/jay_m)\
**Post date:** [December 27, 2020, 10:06am UTC](https://discourse.processing.org/t/void-function-is-returning-values-but-shouldnt/26528/5 "2020-12-27T10:06:38Z")

</div>

**sub** is not `void`. It Returns a PVector as per the documentation

> **[PVector::sub() \\ Language (API) \\ Processing 3+](https://processing.org/reference/PVector_sub_.html)**

---

<div class="post-metadata">

**Author:** ![Divitiacus](https://avatars.discourse-cdn.com/v4/letter/d/848f3c/32.png) [@Divitiacus](https://discourse.processing.org/u/Divitiacus)\
**Post date:** [December 27, 2020, 12:43pm UTC](https://discourse.processing.org/t/void-function-is-returning-values-but-shouldnt/26528/6 "2020-12-27T12:43:16Z")

</div>

This is not about the sub function but about the apply function in the code above. It is called with wind as input parameter but I did not expect that would change the value of wind, because apply is a void function.

---

<div class="post-metadata">

**Author:** ![jay\_m](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jay_m/32/11020_2.png) [@jay\_m](https://discourse.processing.org/u/jay_m)\
**Post date:** [December 27, 2020, 1:23pm UTC](https://discourse.processing.org/t/void-function-is-returning-values-but-shouldnt/26528/7 "2020-12-27T13:23:15Z")

</div>

got it.

you passed an object as a parameter and apply() modifies that object’s data so when you come back, even if apply() did not return anything, you have all the side effects.
