1. Updated Resource Submission Rules: All model & skin resource submissions must now include an in-game screenshot. This is to help speed up the moderation process and to show how the model and/or texture looks like from the in-game camera.
    Dismiss Notice
  2. DID YOU KNOW - That you can unlock new rank icons by posting on the forums or winning contests? Click here to customize your rank or read our User Rank Policy to see a list of ranks that you can unlock. Have you won a contest and still haven't received your rank award? Then please contact the administration.
    Dismiss Notice
  3. Rubbed the right way, the genie is out of its lamp! The 12th Concept Art Contest Results have been announced.
    Dismiss Notice
  4. Race against the odds and Reforge, Don't Refund. The 14th Techtree Contest has begun!
    Dismiss Notice
  5. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

[JASS] Multiple return statements

Discussion in 'Triggers & Scripts' started by Frostilicus, Sep 7, 2004.

  1. Frostilicus

    Frostilicus

    Joined:
    Aug 24, 2004
    Messages:
    11
    Resources:
    0
    Resources:
    0
    What is the purpose of back to back return statements in a function? I'll use an example from fugly's Create Aura script to show you what I'm talking about:

    Code (Text):
    function H2G takes handle h returns group  
       return h
       return null
    endfunction
    I have seen quite a few people do this in their code, but I don't understand the purpose. I can only assume that if the first return statement fails Warcraft will attempt the next command, kind of like a try/catch clause in C++. But I NEVER see anybody test for the error condition. So what is the point of having a second return statement if your script will only crash again because of a null pointer error in the function that called this function?
     
  2. Tommi

    Tommi

    Joined:
    Jun 16, 2004
    Messages:
    237
    Resources:
    0
    Resources:
    0
    It's a so called "return bug." Only the last return statement must comply to return type. Now if you look at the code, you see that the script actually returns a handle, when it should return a group. ("return null" is never executed; it's there only for the compiler.) Thus, you can use that function to convert a handle to a group type. Hence the name H2G.

    For what purpose such a function is used, that I don't know. Perhaps it has something to do with the fact that "group" is a subtype of "handle".

    Cheers!
     
  3. Frostilicus

    Frostilicus

    Joined:
    Aug 24, 2004
    Messages:
    11
    Resources:
    0
    Resources:
    0
    Exploiting a bug isn't a very good practice because who knows when Blizzard will decide to fix the bug and not tell us. It could easily break tens or hundreds of scripts that I've seen on this site. Have any attempts been made to convince Blizzard to introduce a type-casting system?

    On a side note, it's generally frowned upon to use magicode. Magicode is a segment of code that magically does something which is not immediately apparent. At the very least add a comment above the function describing how it works. I would personally like to see many more comments in the JASS snipetts submitted because it makes learning JASS a little easier. A general description of the snippet as a whole is not enough. It should be broken down function by function, and if needed, line by line. DOWN WITH MAGICODE! :D

    Adding useful comments in the snippets would really cut down on the number of users having problems when they attempt to customize the code for their map.
     
  4. KaTTaNa

    KaTTaNa

    Joined:
    Mar 27, 2004
    Messages:
    70
    Resources:
    0
    Resources:
    0
    People call it "The return bug" because that is what the person who discovered it used to call it.
    Blizzard has stated in their forums that the bug will not be fixed, and Bliz even use the "bug" themselves.

    You better get used to these kind of functions, they are really common and really useful. Besides, no one have ever experienced any side effects by using them.