Sub Procedures vs. Function Procedures
A Sub procedure performs an action but does not return a value, declared with Sub Name(parameters) ... End Sub and invoked as a standalone statement. A Function procedure, declared with Function Name(parameters) As ReturnType ... End Function, computes and returns a value using either Return value or by assigning the value to the function's own name, and must be called in a context that uses its result, such as an assignment or expression.
Cricket analogy: A twelfth man bringing out drinks is like a Sub procedure — DeliverDrinks() performs an action on the field but hands back no result, whereas the third umpire's CheckRunOut() is a Function that returns a definite Out or Not Out verdict used by the on-field umpire.
Parameters and Return Values
Parameters let procedures accept input, and by default VB.NET passes arguments ByVal, meaning the procedure receives a copy of the value (for reference types like objects and arrays, it's a copy of the reference, so mutating the object's contents still affects the caller). A Function returns its result either via an explicit Return statement, which also immediately exits the function, or by assigning the return value to a variable matching the function's own name before End Function, though Return is generally the clearer and preferred style in modern VB.NET.
Cricket analogy: A scorer function CalculateStrikeRate(runs As Integer, balls As Integer) As Double receives copies of the runs and balls values, computes runs/balls * 100, and uses Return to hand back the strike rate to whoever called it, exiting immediately once returned.
Overloading Procedures
VB.NET allows multiple procedures with the same name but different parameter lists — a technique called overloading, explicitly marked with the Overloads keyword when at least one overload is inherited or when mixing with Shadows. The compiler picks the correct overload at compile time based on the number, order, and types of arguments supplied at the call site, letting a method like Area work sensibly whether it's given a single radius, or both a width and a height.
Cricket analogy: A stats app overloads CalculateAverage(runs As Integer, innings As Integer) As Double and CalculateAverage(runs As Integer, innings As Integer, notOuts As Integer) As Double, letting the compiler pick the version matching whether not-out innings were tracked separately.
Scope, Calling Conventions, and Access Modifiers
Procedures declared Public are visible outside their module or class, Private restricts them to the containing type, and Friend limits visibility to the same assembly — choosing the narrowest scope that still works is good practice for encapsulation. VB.NET also allows the optional Call keyword before invoking a Sub (Call MyProcedure() versus just MyProcedure()), though modern style typically omits it except when calling a procedure and deliberately discarding a Function's return value in a statement context.
Cricket analogy: A franchise keeps its data-scouting algorithm Private to the analytics department while releasing player fitness stats as Public to broadcasters, mirroring how VB.NET restricts internal helper Subs while exposing key Functions to the rest of the program.
Module ProcedureDemo
Sub Main()
Call PrintWelcome("Ava")
Dim total As Double = CalculateArea(5.0)
Console.WriteLine($"Area: {total}")
Console.WriteLine($"Area (rect): {CalculateArea(4.0, 6.0)}")
End Sub
' Sub: performs an action, returns nothing
Private Sub PrintWelcome(name As String)
Console.WriteLine($"Welcome, {name}!")
End Sub
' Function: overload 1 - circle area
Public Function CalculateArea(radius As Double) As Double
Return Math.PI * radius * radius
End Function
' Function: overload 2 - rectangle area
Public Function CalculateArea(width As Double, height As Double) As Double
Return width * height
End Function
End ModulePrefer Return value over assigning to the function's own name inside the function body. Return makes control flow explicit and exits immediately, while the older name-assignment style can silently continue executing remaining code after the 'return' value is set, leading to subtle bugs.
Overloaded procedures must differ in their parameter list (number, order, or types) — you cannot overload solely by changing the return type or parameter names. Attempting to declare two Functions with identical parameter signatures but different return types will not compile.
- A Sub performs an action and returns no value; a Function computes and returns a value via Return.
- Parameters are passed ByVal by default in VB.NET, giving the procedure a copy of the value or object reference.
- Return immediately exits a Function with the specified value; it is preferred over assigning to the function's own name.
- Overloading lets multiple procedures share a name but differ by parameter count, order, or type, resolved at compile time.
- Access modifiers Public, Private, and Friend control a procedure's visibility from other code.
- The Call keyword is optional before invoking a Sub and is rarely used in modern VB.NET style.
- Choosing the narrowest workable access modifier for a procedure supports better encapsulation.
Practice what you learned
1. What is the key difference between a Sub and a Function in VB.NET?
2. By default, how are arguments passed to a VB.NET procedure?
3. What determines which overloaded procedure the compiler calls?
4. Which access modifier restricts a procedure's visibility to the same assembly?
5. What happens immediately when a Return statement executes inside a Function?
Was this page helpful?
You May Also Like
Optional and ByRef Parameters
Learn how VB.NET's Optional, ByRef, and ParamArray parameters give procedures flexible, efficient, and expressive argument-passing options.
Conditionals in VB.NET
Learn how to control program flow in VB.NET using If...Then...Else, Select Case, and logical operators to make decisions based on data.
Error Handling with Try/Catch
Learn structured exception handling in VB.NET using Try, Catch, Finally, and Throw to write robust code that gracefully handles runtime errors.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics