Advertisement

returning without ending the function?

Started by February 24, 2002 11:53 PM
14 comments, last by Lohrno 22 years, 9 months ago
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;} 
[ PGD - The Home of Pascal Game Development! ] [ Help GameDev.net fight cancer ]
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
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
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
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
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
Yeah Useless Hacker''s code seems the best to accomplish what I
wanted...hmm...

-=Lohrno

This topic is closed to new replies.

Advertisement