Anda di halaman 1dari 23

SUB FUNCTION & PROCEDURES

1
INTRODUCTION TO VISUAL BASIC PROGRAMMING

BY : RAZIF MUSTAPHA

F 3005 VISUAL BASIC PROGRAMMING

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.

SUB PROCEDURES EXAMPLE

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.

FUNC PROCEDURE EXAMPLE

HOW TO CREATE A FUNCTION


A function is more or less the same thing as a Sub - a segment of 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:

HOW TO CREATE A FUNCTION


First, we've changed the word "Sub" to "Function"; second

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):

HOW TO CREATE A FUNCTION

HOW TO CREATE A FUNCTION


This code is not doing much good by itself. We need

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:

SUB PROCEDURES VS FUNCTION PROCEDURES


Sub Procedures Function Procedures

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

CREATING AN OBJECT FROM A CLASS


Our function is not much use until we can get it up

and running. Here, "Get it up and running" means create an object from our class.

CREATING AN OBJECT FROM A CLASS

CREATING METHODS THAT DON'T RETURN A VALUE


Created a Method that returned a value. But you

Methods do not have to return a value. In which case, you create a Sub and not a Function

CREATING METHODS THAT DON'T RETURN A VALUE

ANOTHER EXAMPLE

ARGUMENT PASSING ByVal AND ByRef


In Visual Basic, you can pass an argument to a procedure

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.

ARGUMENT PASSING ByVal AND ByRef


Add a new button the form you created in the previous

section. Double click the button and add the following code:

You'll get a wiggly line under

IncrementVariable(Number1).

ARGUMENT PASSING ByVal AND ByRef


To get rid of it, add the following Subroutine to your code:

When you're done, run the programme and click your new

button. What answer was displayed in the message box?

ARGUMENT PASSING ByVal AND ByRef

It should have been 10. But hold on. Didn't we increment the variable Number1 with this line?

ARGUMENT PASSING ByVal AND ByRef


So Number1 started out having a value of 10. After our Sub got called,

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.

Change the parameter to the this:

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.

Anda mungkin juga menyukai