keeping the function running after the return.

Hi.

Is there a way to keep a function running after the it has returned the value?

Let's say I want to calculate the forces on a particle, return them for further use with other function and I also want to calculate the effect of those forces. Can I return the forces and keep the function running?

Thank.

Yotam
No, just do whatever you have to do before returning.
Unless there is some trick I'm not aware of, return should always do what its name implies- ie, return control to the calling function. You may want to rethink your methodology from an organizational standpoint. Functions should have clear purposes to increase code comprehension. If you're creating a function called "Calculate Force and Calculate Effects of Forces," is it possible to split it up into two separate functions?
No, but you may want to yield control.

To do this, you need a class to keep state -- remember how the particles are currently affected between calls to the member function.

Your final code may look something like:
1
2
3
4
5
6
7
8
9
Particle p( ... );

z = p.run( 1500 ); // run particle system for one and a half seconds

use z, p.fx, p.fy, or whatever here.

z = p.run( 500 ); // run another half second...

...

Hope this helps.
Topic archived. No new replies allowed.