Subs, Functions, and Parameters...A beginner's guide.

Submitted on: 1/16/2015 5:41:00 PM
By: Matthew Roberts (from psc cd)  
Level: Beginner
User Rating: By 4 Users
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0, VB Script, ASP (Active Server Pages) , VBA MS Access, VBA MS Excel
Views: 316
     Explains the concepts of Subs, Functions, and Parameters. If you are a little fuzzy on the difference, take a look.


Sometimes we miss the obvious

Using Subs and Functions in VB


Sometimes we miss the obvious. I know there are a lot of things I was doing in Visual Basic that I thought were not only right, but extremely clever. What I eventually discovered was that the reason I was having to dream up such clever “work abounds” was that I was basing my code on incorrect assumptions about how VB works.


One area that I had trouble with, as have several people I have taught coding to is parameters and return values. This article is in the Beginner category, but I didn’t learn some of this stuff until long after I was working as a VB programmer and considered myself a “professional”. If there is one thing I have learned about programming, it is that you NEVER know everything, and there is always another way to do something. My goal is to show programmers the correct way to use these features before they go off and invent ways that will cause problems later. Nothing is worse than realizing that you have been doing something the wrong way for the last three years.


Is this tutorial for you? Let’s find out. Consider the following questions:


          Can you explain the difference between a Sub and a Function in one sentence?

          Can you explain the difference between a PUBLIC Sub and a PRIVATE sub?

          Do you know how to get a return value from a procedure without using global variables?



If you answered “Yes” to all of the questions above, you probably don’t need this tutorial. But before you close it out, you should be sure you really understand how to do these things. Why? Because these skills are fundamental to good coding. You simply cannot write good complex code without them.


If the questions above left you scratching your head in confusion, hang in there. You are not alone. If you read this entire tutorial, I promise they will be answered in a way that you can understand them (or your money back!)


Subs and Functions- What is the difference?


To understand what a sub or function is and why we need them, we should go back a few years. For the moment, we will concentrate on Subs, since they are easier to understand. I will then show you how Functions extend the capability of Subs.


If there are any old line programmers out there, you may remember what we now call “spaghetti code”. Of course, when we were writing it, we didn’t call it that. We called it sheer genius. The term “spaghetti code” refers to the logic path of an application written in a language such as BASIC or BASICA. In these languages, each line had a line number, and you controlled program flow by referring to the associated line number of the command you wanted to execute. (Boy, I am suddenly feeling old here!)


For example:


10 CLS ‘Clears the screen…we are talking DOS here.

20 LN$=INPUT$ “What is your last name?:”

30 FN$=INPUT$ “What is your first name?

40 IF LN$=”” Then GOTO 20

50 IF FN$=”” THEN GOTO 30

60 PRINT “You entered “ + LN$ + “ “ + FN$

70 END


This is a complete (albeit simple) program for BASIC. It prompts for a last name and a first name, then forces you to re-enter it if you didn’t enter a value for one or the other. The part I would like you to notice is the GOTO statement. You may have seen this command used in VB in error handling, and if you have been around any VB programmers very long, you have heard them harp on how evil the GOTO command is.


In BASIC, GOTO redirected program flow to a specific line number. Otherwise, programs started at line 0 (or 10 in most cases) and executed sequentially until they met an END statement, which terminated the program. The problem with this concept was that once you got about 2000 lines of code, it became difficult to track where the program would jump to in any situation. For example, the statement GOTO 20150 meant that to track execution, you had to scroll all the way down to line 20150. Of course, it may contain an IF THEN statement that sent it back to line number 1220. It isn’t hard to imagine why this type of code earned its nickname. Eventually it reached “critical mass” and became unmanageable.


Most BASIC programmers eventually discovered the GOSUB statement. This was a pretty big leap in program control. Like GOTO, it redirected program execution to a line number somewhere in the thousands of lines of code, but unlike GOTO, GOSUB knew where it was redirected from and could return to that point in the program when it completed its task. It told BASIC “GOTO A BLOCK OF SUB-CODE”. Here is how it was used:

10 CLS

20 X$=INPUT$ “Choose a Menu Item”

30 IF X$=”A” THEN GOSUB 2000

40 IF X$=”B” THEN GOSUB 3000





2000 CLS

2010 …..DO STUFF….



3000 CLS

3010 …..DO STUFF….




In this example, we not only use the GOSUB keyword, but we also use the RETURN keyword. What this told BASIC was “Go back to the last GOSUB statement that you executed. “ To programmers, this was pure gold. It allowed recursion (calling a piece of code within the same piece of code) and much better program flow control. In essence, this is the origin of the Visual Basic “Sub” procedure.


OK…enough nostalgia for now. Back to VB. What this history lesson has shown you is that things could be much worse. But with Visual Basic, some obvious improvements have been made. Line numbers have been dropped (a tough concept for us old timers…QuickBasic helped ease us into this concept). This allowed the programmer to name blocks of code with a name instead of a number. Now instead of GOSUB 2000, we can say “Call GetCustomerID” to call a piece of code that will find a customer ID for us.


Here is how the VB Sub works:


First, you need to know what your sub will be doing. There are some basic criteria to determine whether or not you need to place code into a sub. They are:


1.                   Is this code used in more than one place (are you writing duplicate code in your application)?

2.                   Does this code perform a specialized function independent of the rest of the code?

3.                   Can you effectively create this as a “stand-alone” piece of code?


Of the three criteria above, number three is the toughest. For example, if you have a piece of code to find state that a customer is located in based on their zip code, do you write that same piece of code for every customer? Obviously, that would be impractical if not impossible. What you need is a way to find the state any zip code. Here is the way most beginners handle this problem:


First, create public variables to hold the values we will be working with:


Public ZIP As String

Public State As String


Public Sub GetState()


If ZIP > 32501 AND ZIP <34205 Then

State = “MS”

ElseIf ZIP >45102 AND ZIP < 53210 Then

State = “TN”



End Sub


This code works. Using this method, you can get the state of any of the zip codes contained in the GetState sub. But there are problems with this code as well. The main one is that you are now relying on public variables. This is because you have to be able to access the values in from your form or calling code and within your sub. This can get real messy real quick when you consider that you may need to use the name “state” for a variable many times in an application. You are then forced to create MANY public variables with odd names like GetStateFromZip_State and GetStateFromZip_Zip to insure that you don’t accidentally overwrite your values from other places in your program. This is just a really bad way to code. The solution? Parameters (finally!).


In order to get your values safely to your Sub without having to create public variables, you can instead create Sub Parameters. These are really just variables that only your calling code and your sub can see. They look like this:


Public Sub GetState(ZIP As String)


If ZIP > 32501 AND ZIP <34205 Then

State = “MS”

ElseIf ZIP >45102 AND ZIP < 53210 Then

State = “TN”


End If

End Sub


Now this code does the exact same thing, but without having to rely on the public variable ZIP. You can also pass multiple parameters:


Public Sub AverageNumbers(Number1 As Integer, Number2 As Integer, Number3 As Integer)

End Sub


All three of these values will be available from within your sub, but will not exist outside of it. Getting pretty neat, isn’t it?


But we still have a problem. We passed the variable IN , but how do we get a value back OUT of a sub? I mean, it’s nice that we averaged these numbers, but we still have to use a public variable to get the return value, right? Wrong.


There are three ways to get return values from a piece of code:


1.       Public Variables (ugly!)

2.       By making a “return value” parameter.

3.       By making your sub into a function.


We have already established that public variables are not the answer we are seeking, so lets examine option #2. This is more of a “hack” than a feature of VB. It takes advantage of the fact that both the calling code and the sub code have access to parameter values. You could do this to get a return value:


Public Sub AverageNumbers(Number1 As Integer, Number2 As Integer, Number3 As Integer, ReturnValue As Integer)

ReturnValue = (Number1 + Number2 + Number3) /3


End Sub


Again, this would work. But it creates problems on the calling side now. In order to use it, you have to use code similar to this:

Call AverageNumbers(10, 20, 50,0)

Msgbox “Average = “ & ReturnValue


It is technically returning a value, but it looks (and is) rather clunky. You have to pass in a “0” for the return value or you will get an error. Then, to make matters worse, you have to then refer to that variable when the sub completes. What would be nice is if you could use it just like a VB command. Consider the Ucase() command in Visual Basic. It is straightforward:


MsgBox Ucase(“this is a test”)


Results is a message box being displayed with the words “THIS IS A TEST” in it.


So how does Microsoft do that? How do they get a return value from the Ucase command? Surely it is some secret code that you don’t have the power to duplicate. Wrong! The way they do it is by making the command Ucase() into a Function instead of a Sub. What is the difference? Simple:


A function can return a value through its name variable. A sub cannot.


What does that mean “through its name variable”?


Let’s look at a function declaration:


Public Function AverageNumbers(Number1 As Integer, Number2 As Integer, Number3 As Integer) As Integer



End Function



You should notice two things different about this declaration compared to the sub declaration:

          It has no return parameter.

          It has “As Integer” stuck on the end. What is that all about?


So how in the world does the value of Ucase() (or AverageNumbers for that matter), find its way back to the calling code “MsgBox = “?


Well, that is the difference between Subs and Functions. Functions act as a return value variable. That is why it was declared “As Integer”. You can now call the AverageNumber function exactly as you would the Ucase() function:


MsgBox “Average =” & AverageNumbers(10,20,50)


Here is the complete function:


Public Function AverageNumbers(Number1 As Integer, Number2 As Integer, Number3 As Integer) As Integer


AverageNumbers = (Number1 + Number2 + Number3) /3


End Function



Notice that the line:


ReturnValue = (Number1 + Number2 + Number3) /3


has now been modified to read:


AverageNumbers = (Number1 + Number2 + Number3) /3


This is where the “magic” happens. VB performs the calculations, and when it gets the results, it pipes it back into the AverageNumbers variable which was created when this function was declared. From there, it can be assigned back to a variable in the calling code. So to see the whole picture:


Private Sub Command1_Click()

MsgBox “Average =” & AverageNumbers(10,20,50)

End Sub


Public Function AverageNumbers(Number1 As Integer, Number2 As Integer, Number3 As Integer) As Integer


AverageNumbers = (Number1 + Number2 + Number3) /3


End Function



So now you can start to see how return values work with functions. Once you fully grasp this concept, you will begin to realize that VB commands are nothing but functions written by the Microsoft VB team. Conceptually, yours are no different. You can actually create your own commands to us in your application, just by creating them as functions!


This is big!



There is one more concept I want to touch on before wrapping up:


The difference between Public and Private Subs and Functions


To fully understand the implications of making a function or sub public or private, you will need to study up on the topic of SCOPE in VB. This basically is the level at which something is “visible” to the rest of the application. As you have seen through earlier examples, Public variables are not a good thing. Public Functions, on the other hand, are a VERY good thing. This makes them accessible from anywhere in your application…so if you want to average a number from three different forms, you can still call the same AverageNumbers function.


With Private subs and functions, you will only be able to call them from within the object that they are declared in. Why would you want to do this? Well, you may have noticed that VB creates all Form Subs as Private. This is because if you created them as Public, you would have many Form_Load() subs, many Command1_Click () subs, etc. This would make your application crash instantly, so by using private scope, you effectively “hide” these subs from other forms.


ONE NOTE: You cannot declare a Public variable, sub, or function from within a form. YOU MUST DECLARE ALL PUBLIC ITEMS FROM WITHIN A MODULE. You can add a module to your project by going to the Project menu and clicking “Add Module”.


I sincerely hope this tutorial has helped you in grasping how and why to use subs and functions. This is such a vital topic to good programming and so little is published about it. Please let me know if you need more information on any of the topics covered in this tutorial.



Other 25 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.