9 Replies Latest reply on Sep 12, 2011 11:49 PM by kglad

    'return;' help :)

    McbainGames11 Level 2

      Hey,

       

      Just wondering why people use return in a class like this:

       

       

      package  {
         
          public class myClass extends MovieClip {

       

              public function myClass_One() {
                 
                  return;
                 
              }

       

          }
         
      }

       

       

       

       

      what does it do... ? I examine peoples projects to see how it works and maybe learn something and i see about 10 files of that code with diffrent class's on most projects so i would like to know what it does

       

      Thanks

        • 1. Re: 'return;' help :)
          kglad Adobe Community Professional & MVP

          anyone that uses that isn't someone you want to emulate.   it does nothing.  removing "return;" would be clearer coding.

          • 2. Re: 'return;' help :)
            McbainGames11 Level 2

            I was thinking it might do something if it being extended somewhere else and that return is returning something from another class, if that a possibility because i doubt they would have 10 files in a project for nothing and i see this on every project i look at on cool games?

             

            Thanks for helping.

            • 3. Re: 'return;' help :)
              McbainGames11 Level 2

              Here we go... i extracted your fla for one of your tutorials on your site it is the 'hit test' one and this is of 2 of 3 class in there:

               

              package
              {
                  import flash.display.*;

               

                  public class buttonShadeUp extends MovieClip
                  {

               

                      public function buttonShadeUp()
                      {
                          return;
                      }// end function

               

                  }
              }

               

               

              package
              {
                  import flash.display.*;

               

                  public class buttonShadeDown extends MovieClip
                  {

               

                      public function buttonShadeDown()
                      {
                          return;
                      }// end function

               

                  }
              }

               

               

              my question is basically why was there a need for that?

              • 4. Re: 'return;' help :)
                Andrei1 Level 6

                return indicates the end of the function and it does return datatype that function is meant to, well, return.

                 

                In AS3 when function datatype is void - there is no need to write it explicitly - compiler will "write" it for you.

                 

                When you indicate that function returns other than void datatype - you will have to write return line.

                 

                Examples you show do not cover one aspect of return. You can end function execution at any point by simply writing return statement. In the following code (not very practical) function will seize to execute at three different points.

                 

                Note that the third conditional checks of argument is greater than 0. If you pass value of 1 although it is greater than 0 - third statement will not be executed unless value is greater than 1:

                 

                function myFunction(integer:int = 0):void {
                     if (integer == 0) {
                          trace("zero was passed");
                          return;
                     }
                     if (integer == 1) {
                          trace("one was passed");
                          return;
                     }
                     if (integer > 0) {
                          trace("greater than 1 is passed");
                          return;
                     }
                     trace("got to the end");
                }
                
                • 5. Re: 'return;' help :)
                  kglad Adobe Community Professional & MVP

                  1.  i don't have any tutorials on my site

                  2.  i didn't created those classes

                  3.  that's not my code style and

                  4.  you didn't download that from kglad.com

                   

                  however, you may have downloaded a swf from my site, decompiled it and the decompiler created that file and possibly other trash.

                  • 6. Re: 'return;' help :)
                    McbainGames11 Level 2

                    What advantage does this give you because you could simply make tht integer a variable without return?

                    • 7. Re: 'return;' help :)
                      kglad Adobe Community Professional & MVP

                      if your question is whether the "return" statement has any use, that's quite a bit different than your original question about its use in a constructor where you can return no value and you shouldn't be doing anything other than initializing your class.

                       

                      the "return" can be used to eliminate using if-else statements allowing you to use a sequence of if-statements.

                      • 8. Re: 'return;' help :)
                        McbainGames11 Level 2

                        Yes this was a sort of 2 questions in one

                         

                        That constructor class one must be my decompiler adding it.

                         

                        Thanks your help.

                        • 9. Re: 'return;' help :)
                          kglad Adobe Community Professional & MVP

                          you're welcome.