1

I have a situation where I'd like to use GoSub within a Subroutine and in some situations Return, but in others I would not have it Return. This will be in a large be For Loop and GoSub without Return could happen potentially hundreds of times. If I do not Return will this build up in memory and cause any issues?

I suppose my question boils down to: does GoSub stack? And will a large enough stack of un-returned GoSub's cause problems?

If it does stack, I can change the code to use GoTo in the instances where I do not want to Return, but for simplicity's sake I'd rather not.

Also thank you in advance for not lecturing me on GoTo/GoSub not being best practice :)

1
  • 5
    Don't do it. Write an actual Sub or Function instead, then call that. You can predict that behavior trivially. The fact that you can't do the same with GoSub is exactly the reason not to use it.
    – Comintern
    Oct 8, 2018 at 15:16

2 Answers 2

2

As plenty of others said: Don't do it. I am now programming (for a living) since 30 years and never had the need to use GoSub/Return except as a substitute when a programming language didn't provide any subroutines.

That said - I was curious about how VBA handles this. First thing: I assume that there must be a kind of stack. You can have multiple GoSub/Return in one routine and it is handled correctly:

Sub testSub1()
    Call StrangeRoutine1
End Sub

Sub StrangeRoutine1()

    GoSub L1
    Exit Sub

L1:
    Debug.Print "Strange1 - L1a"
    GoSub L2
    Debug.Print "Strange1 - L1b"
    Return

L2:
    Debug.Print "Strange1 - L2a"
    Return
End Sub

This Prints:

Strange1 - L1a
Strange1 - L2a
Strange1 - L1b

So the return statements jumps after the correct GoSub - this is for sure handled with a kind of stack.

However, it seems that this stack is cleared once a Subroutine is left. The following routine has a GoSub, but no Return. So it leaves an open GoSub on the "stack". But when called a 2nd time and issues a Return without an GoSub, it throws an runtime error 3 "Return without GoSub`.

Dim count As Long

Sub testSub2()
    For count = 1 To 2
        Call StrangeRoutine2
    Next
End Sub


Sub StrangeRoutine2()
    If count > 1 Then Return
    GoSub L1
    Exit Sub

L1:
    Debug.Print "Strange2 - L1"
End Sub

P.S.: Did i mention it: Don't do it!

3
  • I suspect the "stack" is scoped to the procedure, based on the fact that both instructions are required to be in the same procedure. If it did (and allowed jumping into and out of procedures), you'd probably end up trashing the real stack.
    – Comintern
    Oct 8, 2018 at 16:15
  • Ah ha! It does stack then, okay thank you. I've run my own test on running a GoSub 50,000 times without using Return, then using Return 10,000 times without calling an additional GoSub and each Return was successful. As long as one does not call Return more than they have called GoSub it appears to work fine, and does not cause any issues. My use case will be more like 1,000 maximum, so my 50,000 test shows that it will function correctly - even if it's not best practice ;)
    – Mike Lasch
    Oct 8, 2018 at 18:30
  • 1
    You can trivially confirm this with Foo: GoSub Foo, which results in a runtime error 28 - Out of stack space.
    – Comintern
    Oct 9, 2018 at 15:32
1

The shortcut to see the stack in VBE is Ctrl+E. As you see it does not stack:

enter image description here

Sub GosubDemo()

    GoSub MyRoutine
    Debug.Print "Line before Exiting"
    Exit Sub

GoSomeWhereElso:
    Debug.Print "SomewhereElso I am "
    Return

MyRoutine:
    Debug.Print "My routine"
    GoSub GoSomeWhereElso
    Return

End Sub

However, do not use GoSub or GoTo in VBA. It is considered a very bad practice. GoTo could be used for Error Handling like On Error GoTo ErrorHandler.

8
  • 2
    The problem in a loop is this (from the MSDN link): "A subroutine can contain more than one Return statement, but the first Return statement encountered causes the flow of execution to branch back to the statement immediately following the most recently executed GoSub statement." If you GoSub and don't Return, you can't "clear" your current GoSub context. OP's code sounds like a nightmare to debug and maintain.
    – Comintern
    Oct 8, 2018 at 15:22
  • 1
    I had to write the bad practice sentence in the answer, sry.
    – Vityata
    Oct 8, 2018 at 15:22
  • 1
    @Comintern - Actually, "nightmare to debug and maintain" usually is how a lot of VBA developers build their code. With integrated "job defence" within it. Then the new developer really wants to cry once he start working with legacy code (I remember I wanted to cry after seeing 5000 lines in 5 subs and 1 function.) And there is no documentation, because "we do not have time for this and the code itself is the best documentation". And yes, there were a lot of GoTo statements there. (Disclaimer - nothing against the OP, most probably the case is different.)
    – Vityata
    Oct 8, 2018 at 15:36
  • 1
    @Vityata: I feel your pain. I'm going through that right now! Just don't get why "developers" can't just split into smaller functions or subs.. it's a pain going through legacy code!
    – Zac
    Oct 8, 2018 at 15:48
  • 1
    Well I was hoping the last sentence in my post would take care of this conversation but I suppose it was inevitable, hahaha. Thanks for answering my question @Vityata!
    – Mike Lasch
    Oct 8, 2018 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.