A procedure is a block of on e or more code statements. All Visual Basic .NET code is written within procedures.
This module discussed two types of procedures, subroutines and functions. The difference between them is that functions return a value, whereas subroutines do not.
Visual Basic .NET has many built-in procedures. Some, such as event procedures, are subroutines. Others, such as the InputBox function, are functions.
Visual Basic .NET also enables you to create your own procedures. There are several reasons why you might want to create your own procedures. Your code is more readable if divided up among several smaller procedures than if it is all put in to one procedure that contains pages of code. Additionally, if you are performing essentially the same task from several places in the program, you can avoid duplication of code by putting the code that performs that task in one place, as opposed to repeating that code in each place in the program that may call for the performance of that task. Further, if you later have to fix a bug in how you perform that task, or simply find a better way to perform the task, you only have to change the code in one place rather than many.
Procedures don't execute by themselves. They need to be called. Subroutines are called by name, with any arguments in parentheses. Functions usually are called either on the right side of an assignment statement or in an expression, often with an If...Then control structure.
You use arguments to pass information to procedures. The arguments may be passed by value, using the ByVal keyword, or by reference, using the ByRef keyword. This module explained the difference between these two methods of passing arguments. Additionally, you can use optional arguments and the ParamArray argument. Finally, this module showed you how to overload procedures so closely related procedures can share the same name, differing only by their argument list.
Was this article helpful?