Sub and Function Procedures
Sub and Function Procedures
Sub and Function Procedures
1
INTRODUCTION TO VISUAL BASIC PROGRAMMING
BY : RAZIF MUSTAPHA
SUB PROCEDURES
Is a series of VB statements enclosed by the Sub and End
Sub statements. It performs a task and then returns controls to the calling code, but does not return a value to the calling code. Each time the procedure called, its statements are execute starting with first executable statements after the Sub statement and ending with the first End Sub, Exit Sub or Return statements encountered
SUB PROCEDURES
User can defines procedure in modules, classes and
structures Public by default, can call it from anywhere in the application that has access to the module, class or structure where it is defined Can take arguments constant, variables, expressions which are passed to it by the calling code.
FUNCTION PROCEDURES
Is a series of VB statements enclosed by the Function and
End Function statements. It performs a task and then returns controls to the calling code and return a value to the calling code. Each time the procedure called, its statements are execute starting with first executable statements after the Function statement and ending with the first End Function, Exit Function or Return statements encountered
FUNCTION PROCEDURES
User can defines Function procedure in modules,
classes and structures Public by default, can call it from anywhere in the application that has access to the module, class or structure where it is defined Can take arguments constant, variables, expressions which are passed to it by the calling code.
you create yourself, and that can be used whenever you want it. The difference is that a Function returns a value, while a Sub doesn't. When you Called a Sub you did this:
Visual Basic will go off and execute that code for you, and then drop
down to the next line. The Sub AddNumbers is not a value, it's not equal to anything. It's not like a normal variable where you assign something to it. It's just the name of your Subroutine. A Function is different. It is a value, will be equal to something, and you do have to assign a value to it. You create a Function in the same way you did a Sub, but this time your code will be like this:
we've added "As" something, in this case "As Boolean". The name we called our Function is ErrorCheck, and ErrorCheck is now just like a variable. And just like a variable, we use one of the Types. We can use "As Integer", "As Long", "As Double", "As String", or any of the variable types.
Let's write some code, and try an example. Add a new button and a textbox to your form. Change the
Name of the textbox to txtFunction. Double click your button and add the following code to it (add it after the End Sub of the button, but before the End Class):
a way to use it. This time, because we've set up a Function, we have to assign the value of the function to a variable. So double click your button and add the following:
Enclosed by the Sub and End Sub Enclosed by the Function and End statements Function statements Does not return a value to the calling code its statements are execute after the Sub statement and ending with the first End Sub, Exit Sub or Return statements Return a value to the calling code
its statements are execute after the Function statement and ending with the first End Function, Exit Function or Return statements
and running. Here, "Get it up and running" means create an object from our class.
Methods do not have to return a value. In which case, you create a Sub and not a Function
ANOTHER EXAMPLE
by value or by reference by specifying the ByVal or ByRef keywords, respectively The word ByVal is short for "By Value". What it means is that you are passing a copy of a variable to your Subroutine. You can make changes to the copy and the original will not be altered. ByRef is the alternative. This is short for By Reference. This means that you are not handing over a copy of the original variable but pointing to the original variable. Let's see a coding example.
section. Double click the button and add the following code:
IncrementVariable(Number1).
When you're done, run the programme and click your new
It should have been 10. But hold on. Didn't we increment the variable Number1 with this line?
we added 1 to Number1. So we should have 11 in the message box, right? The reason Number1 didn't get incremented was because we specified ByVal in the Sub:
This means that only a copy of the original variable got passed over.
When we incremented the variable, only the copy got 1 added to it. The original stayed the same - 10.
Run your programme again. Click the button and see what happens. This time, you should see 11 displayed in the message box. The
variable has now been incremented! It was incremented because we used ByRef. We're referencing the original variable. So when we add 1 to it, the original will change. The default is ByVal - which means a copy of the original variable. If you need to refer to the original variable, use ByRef.