14 Replies Latest reply on May 11, 2016 3:15 PM by Mike_Mitchell

    Subroutine

    lmarcinelli

      In a script, can I call a subroutine that is also in that same script?

       

      For example:

       

      If [ $WhichOne = 1]

      if [not checkdate()]

      halt script

      end if

       

      Set field

       

      Else if [$WhichOne = 2]

      if [not checkdate()]

      halt script

      end if

       

      set field

       

      End if

      End if

      _____________________

       

      # subroutine

      checkDate ()

      return 1

      if date= "" then

      show custom dialog [" Error "; "Need Date"]

      if [ Get ( LastmessageChoice ) = 2 ] then

      return 0

      end if

      end CheckDate

       

       

      Thanks in advance!

        • 1. Re: Subroutine
          Mike_Mitchell

          No, you would need to write a second script and call that. You can do that using a combination of Script Parameters and the Exit Script script step (I recommend against using Halt Script unless you have a really good reason, because it kills the entire call stack).

           

          Example:

           

          If [ isEmpty ( dateField ) ]

               Show Custom Dialog [ "Error" ; "Need Date" ]

               If [ Get ( LastMessageChoice = 2 ]

                    Exit Script [ 0 ]

               End If

          End If

          Exit Script [ 1 ]

          1 of 1 people found this helpful
          • 2. Re: Subroutine
            erolst

            Mike_Mitchell wrote:

             

            No, you would need to write a second script and call that.

            Not necessarily:

             

            If [ IsEmpty ( Get ( ScriptParameter ) ) // regular operation ]

              if [ someCondition ]

                 do stuff

               else

                 do other stuff

              End

              Perform Script [ MyScript; parameter: "cleanUpAndExit" ] ]

            End

             

            If [ Get ( ScriptParameter ) = "cleanUpAndExit" ]

              # [ delete global $$vars, do other stuff ]

              Exit Script [ ]

            End If

             

            which is more a simulation of a sub-routine call, since the script is entered into the stack a second time. But this is a valuable technique to pack related functionality into a single script. (And maybe one day there will be code folding …)

            • 3. Re: Subroutine
              lmarcinelli

              again.. so messy to not be able to call a subroutine in the same script

              • 4. Re: Subroutine
                Mike_Mitchell

                It's actually in keeping with more modern coding standards. Each module is expected to be self-contained, receive parameters, and return a result. The old "subroutine" practices of days past have largely been abandoned by the industry.

                • 5. Re: Subroutine
                  Mike_Mitchell

                  What part of "cleanUpAndExit" isn't a second script?

                  • 6. Re: Subroutine
                    Mike_Mitchell

                    I'm not sure what you mean by "messy", but this is common practice now. It allows said "subroutine" to be usable by other scripts besides the one that called it.

                     

                    What language are you accustomed to?

                    • 7. Re: Subroutine
                      erolst

                      This is supposed to be code from a single script, so the answer would be "none"

                      • 8. Re: Subroutine
                        lmarcinelli

                        I try to use as few lines as possible, so the code is clean.  I get the objects and passing parameters but I would prefer to keep subroutines in the same script if won't be called by any other.

                         

                        C++, Macromedia Director,  a little HTML5 and Xcode but I am not a coder anymore...  I recently learned Filemaker for a quick project. I am sure I am not using it efficiently; hence the questions to check.

                         

                        Thanks again for your help.

                        • 9. Re: Subroutine
                          Extensitech

                          Script is calling itself. "cleanUpAndExit" is a parameter, not another script.

                           

                          I saw it the way you did the first time I read it, too.

                           

                          Chris Cain

                          Extensitech

                          • 10. Re: Subroutine
                            Mike_Mitchell

                            Yeah, it would help if erolst would say the script is called "MyScript" ...  

                             

                            But I stand corrected. I've actually used a similar technique when running a scenario where the script needed to be re-executed for some reason.

                            • 11. Re: Subroutine
                              lmarcinelli

                              Thanks everyone for taking the time to help me. You guys are great!

                              • 12. Re: Subroutine
                                erolst

                                Mike_Mitchell wrote:

                                Yeah, it would help if erolst would say the script is called "MyScript" ...

                                Sorry Mike and Chris, I try to be clearer in the future.

                                 

                                As an aside: since we officially have line numbers since v14, how about GOTO?

                                • 13. Re: Subroutine
                                  Mike_Mitchell

                                  Don’t spread rumors like that! It’ll ruin my reputation!  

                                   

                                  Glad we can help.

                                  • 14. Re: Subroutine
                                    Mike_Mitchell

                                    I can see cases where that could be useful.