A procedure is a block of Visual Basic statements enclosed by a declaration statement (
Set) and a matching
End declaration. All executable statements in Visual Basic must be within some procedure.
Calling a Procedure
You invoke a procedure from some other place in the code. This is known as a procedure call. When the procedure is finished running, it returns control to the code that invoked it, which is known as the calling code. The calling code is a statement, or an expression within a statement, that specifies the procedure by name and transfers control to it.
Returning from a Procedure
A procedure returns control to the calling code when it has finished running. To do this, it can use a Return Statement, the appropriate Exit Statement statement for the procedure, or the procedure's End <keyword> Statement statement. Control then passes to the calling code following the point of the procedure call.
Returnstatement, control returns immediately to the calling code. Statements following the
Returnstatement do not run. You can have more than one
Returnstatement in the same procedure.
Exit Functionstatement, control returns immediately to the calling code. Statements following the
Exitstatement do not run. You can have more than one
Exitstatement in the same procedure, and you can mix
Exitstatements in the same procedure.
If a procedure has no
Exitstatements, it concludes with an
End Get, or
End Setstatement following the last statement of the procedure body. The
Endstatement returns control immediately to the calling code. You can have only one
Endstatement in a procedure.
Parameters and Arguments
In most cases, a procedure needs to operate on different data each time you call it. You can pass this information to the procedure as part of the procedure call. The procedure defines zero or more parameters, each of which represents a value it expects you to pass to it. Corresponding to each parameter in the procedure definition is an argument in the procedure call. An argument represents the value you pass to the corresponding parameter in a given procedure call.
Types of Procedures
Visual Basic uses several types of procedures:
Sub Procedures perform actions but do not return a value to the calling code.
Event-handling procedures are
Subprocedures that execute in response to an event raised by user action or by an occurrence in a program.
Function Procedures return a value to the calling code. They can perform other actions before returning.
Some functions written in C# return a reference return value. Function callers can modify the return value, and this modification is reflected in the state of the called object. Starting with Visual Basic 2017, Visual Basic code can consume reference return values, although it cannot return a value by reference. For more information, see Reference return values.
Property Procedures return and assign values of properties on objects or modules.
Operator Procedures define the behavior of a standard operator when one or both of the operands is a newly-defined class or structure.
Generic Procedures in Visual Basic define one or more type parameters in addition to their normal parameters, so the calling code can pass specific data types each time it makes a call.
Procedures and Structured Code
Every line of executable code in your application must be inside some procedure, such as
Button1_Click. If you subdivide large procedures into smaller ones, your application is more readable.
Procedures are useful for performing repeated or shared tasks, such as frequently used calculations, text and control manipulation, and database operations. You can call a procedure from many different places in your code, so you can use procedures as building blocks for your application.
Structuring your code with procedures gives you the following benefits:
Procedures allow you to break your programs into discrete logical units. You can debug separate units more easily than you can debug an entire program without procedures.
After you develop procedures for use in one program, you can use them in other programs, often with little or no modification. This helps you avoid code duplication.
How to: Create a Procedure
Procedure Parameters and Arguments
Generic Procedures in Visual Basic
Objects and Classes