Void function is returning values but shouldn't

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.

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); 
  
}
  • PVector::sub() 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.
1 Like

Hello,

Some references:

:)

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.

1 Like

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

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.

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.