• Advertisement

Archived

This topic is now archived and is closed to further replies.

returning without ending the function?

This topic is 5809 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it possible for a function to return a value without breaking out of the function? And if so, could I ask how? =D -=Lohrno

Share this post


Link to post
Share on other sites
Advertisement
Not in c/c++. You'll have to call another function passing whatever values you want to pass.

Edited by - lessbread on February 25, 2002 1:00:27 AM

Share this post


Link to post
Share on other sites
Pass a value to the function by pointer, and then modify it from within the function. Unless your program involves multiple threads, or has some sort of time dependency along with another process (or something equally ignominious), I don''t know why you would need to, though.

Share this post


Link to post
Share on other sites
Aww...you mean I have to use a reference =( I hate adding
parameters to my functions =(

And no, I''m not using any any goto lines sorry...I may be evil,
but thats just crossing the line!

-=Lohrno

Share this post


Link to post
Share on other sites
umm... no params? How about a global var then? Why dont you describe what you are trying to accomplish?

Share this post


Link to post
Share on other sites
quote:
Original post by Lohrno
Is it possible for a function to return a value without breaking
out of the function?


That doesn''t make any sense. If you haven''t "broken out of the function", then you haven''t returned. What is it that you are trying to achieve?

--

When horses lift their heads up high to look at something, they''re looking far into the distance. To see things that are closer, they lower their heads.

Share this post


Link to post
Share on other sites
there is a problem though. You can only have one function running at a time. So If you want to check what value a funciton has returned from another function, you need to exit the first function so that you can run the second function, totally defeating your purpose. The reason is your processor can only execute on instruction at a time. With your functions being a list of instructions, you cannot have more than one list active at a time.p
The only possible way would be to have your functions run off of somekind of step-wise style that you could regulate from your main function.
In most cases, if there is something you want to do after you return the value you could just make another function to do that stuff.

...maybe dual processors might be able to get two functions running at the same time...

Share this post


Link to post
Share on other sites
It is possible to have multiple functions running "at the same time" (or to simulate the effect), using mechanisms such as threading or asynchronous function call. However, once a function returns, by definition it has completed. I suspect that, as often happens, the OP has a problem, and is assuming a particular solution rather than asking how to approach solving the problem.

--

When horses lift their heads up high to look at something, they''re looking far into the distance. To see things that are closer, they lower their heads.

Share this post


Link to post
Share on other sites
Do you mean something akin to Delphi''s "Result" variable?

If so, you could do:


int MyFunction
{
int Result = 0;

// Code-that-might-change-value-of-Result-but-you-don''t-want-to-return-from

// Some code

return Result;
}

Share this post


Link to post
Share on other sites
Actually, you can to stuff like that. But that involves (non-portable) stack tricks (thunks): If you use the naked specifier to, when you calle a function, the return address is topmost on the stack. So you ''just have to'' pop it and call it... so long as you make sure you don''t smash the stack.

If that''s not what you wanted... well go learn ADA which has a cobegin operation, which does exactly what you want.

Alternatively, if you wanted lazy evaluation (return a ''result'' but do not actually compute it untill needed), you can go play with trees of functors.

Anyways, search the web for these concepts

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
That doesn''t make any sense. If you haven''t "broken out of the function", then you haven''t returned. What is it that you are trying to achieve?


Heh I was trying to set the functions value to a value without
breaking the function...Oh well...I guess if its not possible
I''ll do things a little less elegantly....
What I ended up doing after reading a couple posts saying it
really isnt possible was this: I was trying to make a small
wrapper for the simple Windows stuff. So I wanted to return
an HWND value from a function that sets up the WNDCLASSEX struct
and registers and creates it, then do the ShowWindow, and
UpdateWindow functions in the same function. But I just ended
up writing another function around it to do the same thing.
So my other function just called the other function...but I dont
know if thats really the most efficient thing but I guess it
doesnt matter too much since it wouldnt be in the main loop.

-=Lohrno

Share this post


Link to post
Share on other sites
quote:
Original post by Fruny
Actually, you can to stuff like that. But that involves (non-portable) stack tricks (thunks): If you use the naked specifier to, when you calle a function, the return address is topmost on the stack. So you ''just have to'' pop it and call it... so long as you make sure you don''t smash the stack.



Oof! Now that sounds complicated but I''ll give it a look on the
web heh...Is that a fast way of doing it?

-=Lohrno

Share this post


Link to post
Share on other sites
quote:
Original post by incin
umm... no params? How about a global var then? Why dont you describe what you are trying to accomplish?


I dont like using global variables that much, I use em when
very necessary. It''s because I''m trying to write things in
a reusable way, and a function that requires a global var is not
very reusable IMHO.

quote:
Oriinal post by Oluseyi
Get over it.



Nope. Gonna cry

-=Lohrno

Share this post


Link to post
Share on other sites
Yeah Useless Hacker''s code seems the best to accomplish what I
wanted...hmm...

-=Lohrno

Share this post


Link to post
Share on other sites

  • Advertisement