Invoking return through a function?

Steve D'Aprano steve+python at pearwood.info
Tue Oct 31 01:32:05 EDT 2017


On Tue, 31 Oct 2017 02:34 pm, Chris Angelico wrote:

> On Tue, Oct 31, 2017 at 2:00 PM, Steve D'Aprano
> <steve+python at pearwood.info> wrote:
>> Python has no GOTO, fortunately, but C has at least two, GOTO and LONGJMP.
>> A C macro could, if I understand correctly, jump into the middle of another
>> function. (Yay for spaghetti code!)
> 
> No, I don't think you do understand them correctly - or at least, I
> don't know of any way for a C macro to jump into the middle of a
> function.

I presume a macro could contain a call to longjmp, yes? Since longjmp can jump
into another function (albeit only one which has prepared for it in advance),
so can the macro.

https://stackoverflow.com/questions/21355110/how-to-goto-into-different-function-in-c

And what about assembly? Couldn't you jump into a function from assembly? Of
course the stack will be all wrong, but if you're using assembly you have to
manage that yourself.


> There are three quite different things mentioned here.
> 
> 1) The 'goto' statement, which unconditionally jumps you to another
> location *in the same function*

Yes. And a large enough function can contain everything. If you wanted to
write BASIC-style unstructured code, you could dump everything into one
function and use GOTO.


> 2) setjmp/longjmp, which is not actually a "goto", but more of a
> "multi-level return"

Right-oh. So sort of like an exception then.

So perhaps C is not quite as unstructured as I had initially thought.




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list