0

I thought at this point that the role of functions and subroutines was very clear to me. But now I am not so sure... I see it written all the time

"Functions can return values / subroutines cannot return a value."

and

"a function can only return a single value" (I realize they can return arrays and such too).

But it seems as though I can effectively "return a value from a subroutine" if I pass the "result" variable into the subroutine... Is this considered a "poor practice?" or am I missing some other key concept here...

Method # 1 (Using a Function):

Sub test1()

    Dim x As Integer
    Dim y As Integer
    Dim z As Integer

    x = 2
    y = 3
    z = test2(x, y)

End Sub

Function test2(var1 As Integer, var2 As Integer) As Integer

    test2 = var1 + var2

End Function

Method # 2 (Using a Subroutine):

Sub test3()

    Dim x As Integer
    Dim y As Integer
    Dim z As Integer

    Call test4(x, y, z)

End Sub

Sub test4(var1 As Integer, var2 As Integer, var3 As Integer)

    var1 = 2
    var2 = 3
    var3 = var1 + var2

End Sub
12
  • Those parameters are passed ByRef (the default, as opposed to ByVal) and as such this is the expected result. Most, if not all times, you want ByVal, and as such you should be explicit about it. Commented Nov 25, 2019 at 14:32
  • Function may have a return type and sub - no and if you create a sub to register your function with Application.MacroOptions it will be available on the spreadsheet with =test2(x, y) in the cell. Commented Nov 25, 2019 at 14:34
  • So if I am not interested in using my functions in a cell... Then it seems I could effectively always use subroutines instead of functions... That doesn't seem right... How do I know when I should make one vs the other. Commented Nov 25, 2019 at 14:38
  • 3
    @Tragamor, but it might change how OP codes, which may be more important in this case. Commented Nov 25, 2019 at 14:44
  • 1
    If anything, you should dispense with subs in favor of functions. There is almost always something which can be returned (e.g. some sort of status code) which can be ignored when you don't need it. Some languages such as C get by with just functions. Some of this is really a matter of taste. For a nice discussion for some of these issues I recommend the book "Code Complete" by Steve McConnell. Commented Nov 25, 2019 at 14:49

1 Answer 1

1

Usually, it is bad practice to change the value of a parameter. Just look at you examples - it is obvious that your function does something with the 2 parameters and returns a value (which you write to z). In the second example, you don't see what will happen unless you look to the subroutine - and not only to the function definition, you need to read the complete code so that you can tell what parameter will manipulated and what not.

In software development, when you call a subroutine, you don't want to look at this subroutine - and often it is even not available for you. Let the subroutine do it's job, but without any side effects.

Use a function whenever possible, it keeps your code much more readable. Trust me...

There are (very few) cases when you want to receive more than one result from a subroutine. In that case, I would advice to put explicitly the keyword ByRef in front of the parameter (even if in VBA this is technically not necessary because it's the default). And put a comment that tells why it is the case. You will thank yourself when you look at your code weeks, months or years later.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you... My main concern is making my code readable months/years later.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.