VB.NET Fundamentals: Procedures

Procedures are named blocks of code that perform a specific task. They are a fundamental concept in programming, allowing you to break down complex programs into smaller, manageable, and reusable units. In Visual Basic .NET, procedures can be either Sub procedures (which perform an action but do not return a value) or Function procedures (which perform an action and return a value).

Understanding Procedures

Procedures help in:

Sub Procedures

A Sub procedure is used to perform an action. It does not return a value. The syntax is as follows:

Sub ProcedureName(parameter1 As DataType, parameter2 As DataType, ...)
    ' Code to be executed
End Sub

Example:

This Sub procedure displays a message to the user.

Sub DisplayWelcomeMessage(userName As String)
    Console.WriteLine("Welcome, " & userName & "!")
End Sub

' Calling the Sub procedure
DisplayWelcomeMessage("Alice") ' Output: Welcome, Alice!

Function Procedures

A Function procedure is used to perform a task and return a value. The return type is specified after the parameter list.

Function FunctionName(parameter1 As DataType, parameter2 As DataType, ...) As ReturnDataType
    ' Code to be executed
    ' Assign a value to the function name to return it
    FunctionName = resultValue
End Function

Example:

This Function procedure calculates the sum of two numbers.

Function AddNumbers(num1 As Integer, num2 As Integer) As Integer
    Dim sum As Integer = num1 + num2
    AddNumbers = sum ' Return the sum
End Function

' Calling the Function procedure
Dim total As Integer = AddNumbers(5, 10)
Console.WriteLine("The sum is: " & total) ' Output: The sum is: 15

Parameters and Arguments

Procedures can accept inputs through parameters. When you call a procedure, you pass values for these parameters, which are called arguments.

Parameter Passing Mechanisms:

ByVal (By Value)
A copy of the argument is passed to the procedure. Any changes made to the parameter inside the procedure do not affect the original argument.
ByRef (By Reference)
A reference to the original argument is passed. Changes made to the parameter inside the procedure will affect the original argument.

Example (ByRef):

Sub IncrementValue(ByRef number As Integer)
    number = number + 1
End Sub

Dim myNumber As Integer = 10
IncrementValue(myNumber)
Console.WriteLine(myNumber) ' Output: 11

Scope of Variables

The scope of a variable determines where in your code that variable can be accessed. Variables declared within a procedure have local scope and are only accessible within that procedure.

Important: It's good practice to declare variables with the narrowest possible scope to avoid unintended side effects. Use Dim for local variables.

Overloading Procedures

Procedure overloading allows you to define multiple procedures with the same name but different parameter lists (different number or types of parameters). The compiler determines which procedure to call based on the arguments provided.

Overloading DisplayInfo procedure:

Sub DisplayInfo(name As String)
    Console.WriteLine("Name: " & name)
End Sub

Sub DisplayInfo(name As String, age As Integer)
    Console.WriteLine("Name: " & name & ", Age: " & age)
End Sub

' Calling overloaded procedures
DisplayInfo("Bob")       ' Calls the first overload
DisplayInfo("Carol", 30) ' Calls the second overload
Tip: Use overloading judiciously to improve code readability and flexibility when a similar operation can be performed with different input types or quantities.

Optional Parameters

You can define procedures with optional parameters, which do not need to be provided when the procedure is called. Optional parameters must have a default value.

Sub GreetUser(name As String, Optional greeting As String = "Hello")
    Console.WriteLine(greeting & ", " & name & "!")
End Sub

' Calling with and without the optional parameter
GreetUser("David")         ' Output: Hello, David!
GreetUser("Eve", "Hi")     ' Output: Hi, Eve!