1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

What is the difference between "exit" and "return"?

Discussion in 'Programming' started by MrChoke, Nov 30, 2019.

  1. MrChoke

    MrChoke Member

    Joined:
    Oct 9, 2019
    Posts:
    20
    I know both of them leave the currently executing function. With return you can give a value back to the caller, exit you can't. But with exit, I read there is something about stopping further execution of events maybe?? I didn't quite know what it meant.

    So does exit do anything different than return?
     
  2. TsukaYuriko

    TsukaYuriko Q&A Spawn Camper Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    1,767
    return is usable in scripts. It ends execution and returns a value.

    exit is usable in scripts, events and action blocks and ends their execution, respectively. Call a script in an event, call exit in said script - only the script is ended. Call it in the event - the whole event is ended.
     
    SnotWaffle Studios likes this.
  3. MrChoke

    MrChoke Member

    Joined:
    Oct 9, 2019
    Posts:
    20
    Sorry, I am still confused. When you say "end the event" what do you mean? Let's say I exit out of the "Step" event. I know the rest of execution for "Step" will end. But does that also mean "End step" won't execute?
     
  4. TsukaYuriko

    TsukaYuriko Q&A Spawn Camper Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    1,767
    No, End Step is a different event.
     
  5. MrChoke

    MrChoke Member

    Joined:
    Oct 9, 2019
    Posts:
    20
    Ok, so its the exact same as return. It exits the current function. An event handler is just a function like anything else. I can probably do return in an event handler and it leaves just like exit.
     
  6. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,938
    No, which you would've seen if you'd read TsukaYuriko's first reply.
    There's one more thing you need to think about: return is meant to return a value, exit isn't. If you write myvar = myfunction(foo,bar) and you use exit instead of return, you have basically no idea what value you'll get if you try using myvar later.
     
  7. TsukaYuriko

    TsukaYuriko Q&A Spawn Camper Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    1,767
    You get 0, because squirrel.
     
  8. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,938
    Does the manual say so explicitly or does it just kinda happen? If it's not defined as part of the API, you never know when someone's gonna change it in the future :p
     
    Ido-f and matharoo like this.
  9. TsukaYuriko

    TsukaYuriko Q&A Spawn Camper Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    1,767
    It doesn't, but it also hasn't changed ever since I started using GM. Valid point, though. :p
     
  10. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,938
    There was a time when I used globalvar to avoid having to write "global" for all my global variables, used the way you could address an array without []s to access its zeroeth element, and enjoyed how objects created later were on top of earlier objects meant there was no need to depth-sort bullets. Not to mention using execute_string to avoid having to write scripts unless absolutely necessary. And all of those things meant my GM8 projects were a nightmare to port over to Studio (which is why most of them weren't ported over). I learned my lesson, and these days I only do stuff that's properly defined by the APIs.
     
    TsukaYuriko likes this.
  11. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,136
    You can use return in events, I'm pretty sure. It doesn't actually return anything though outside of a script.

    Isn't exit supposed to end the event even if it's called inside a script in GMS2?
     
  12. nacho_chicken

    nacho_chicken Member

    Joined:
    Jun 21, 2016
    Posts:
    428
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice