Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

is Sub within Sub an accepted practice?



  • Please log in to reply
4 replies to this topic
bruno
  • Members
  • 635 posts
  • Last active: Nov 04 2015 02:26 PM
  • Joined: 07 Mar 2011

i have this main script and subs as follows. Is Sub within Sub an accepted practice?:

 

Main script

...

...

GoSub, L

...

Return

 

L:

L script

...

...

GoSub, L2

...

Return

 

L2:

L2 script

...

...

...

Return



gilliduck
  • Members
  • 109 posts
  • Last active: Nov 09 2015 01:07 AM
  • Joined: 19 Dec 2013

It's fine, but can get a little confusing. Just be sure of where your returns are sending you. If possible I recommend functions over SubRoutines. Helps keep your variables from getting mucked up and can simplify the flow (sometimes). Honestly most anything will work, just how readable it is is up to you.



Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
✓  Best Answer
It is extremely common in programs of any complexity for functions to call functions which call functions etc.  As far as programming theory goes, "subroutine" (which sub is short for) is generally synonymous with "function" (but in AutoHotkey, subs are an older kind of function with different strengths and limitations).

It's not only an accepted practice, but is encouraged to break up your code into logical subroutines if it helps readability, maintainability or code reusability.
  

In different programming languages, a subroutine may be called a procedure, a function, a routine, a method, or a subprogram. The generic term callable unit is sometimes used.
Source: Subroutine - Wikipedia, the free encyclopedia



guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011

"sub within a sub" is not a good practice, and will not even function correctly if your returns are misplaced.

 

however, the example you've given is not a "sub within a sub". your example is merely one sub calling out to another sub, which as Lexikos said is normal

 

here is what i would consider "sub within a sub":

 
;main script
GoSub, L1
return
 
L1:
   ;L1 code
   ;

   L2:
      ;L2 code
      ;

   ;more L1 code
return
 

in that case, L1 would fall into L2 and keep executing until the next return

 

best to use functions instead of subroutines to avoid this confusion



bruno
  • Members
  • 635 posts
  • Last active: Nov 04 2015 02:26 PM
  • Joined: 07 Mar 2011

thanks for the clarification and an additional example of a true "sub within sub".