[try Beta version]
Not logged in

 
keeping the function running after the return.

Jul 15, 2010 at 1:48pm
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
Jul 15, 2010 at 2:09pm
No, just do whatever you have to do before returning.
Jul 15, 2010 at 2:12pm
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?
Jul 15, 2010 at 2:20pm
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.