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?
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?
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...
...