Coding Progress in .NET
.NET,  Coding

VB.NET Difference between a Sub and a Function Procedure

The past two weeks we’ve had a big deadline for my other project – a data coordination related project. So I didn’t get to code much for two weeks. Now with the deadline for the other project behind me I can focus on this coding project I have, yeay!

The coding project I have is more hacking than coding, as I’m refactoring a plugin made in VBA into VB.NET. The syntaxes are fairly similar, but the structure is different.

I started my day with reading up on the difference between a Sub Procedure and a Function Procedure to really understand the concept. With that being said I thought I’d sum up what I learned. Do note that I’m a newbie VB.NET coder trying to keep track of my coding progress and occassionally I have to learn even the most basic stuff on coding. 🙂

What is a procedure?

In VB.NET a procedure is a block of statement enclosed by a declaration statement and a matching end statement. In other coding language you can have closing bracket or closing tag, while in VB.NET you have the very literal and visual END. So coding-wise a procedure can look like this:

Public Sub(ByVal sName as String)

Dim dtDate as Date
dtDate = TimeOfDay()
MsgBox("Hello " & sName & "today is " & CStr(dtDate))

End Sub

There are several types of procedure in VB.NET, but the two common once that I’ve used are a Sub and a Function Procedure.

Since VB.NET is object-based, meaning it’s an Object-Oriented Programming Language. Usually you have a calling code where you call the procedure from and make the procedure perform an action, which then returns control to the calling code

What is a Sub Procedure?

This is a procedure that can perform an action but doesn’t return a value. You can call the Sub Procedure from anywhere in your application as long as your modifier is set to Public or Friend. This way you save code in case you need to repeat the procedure and call it from various area in the application

What is a Function Procedure?

Similar to the Sub Procedure it also can performs an action, which the benefit of returning a value to the calling code.

What is the difference between a Sub and a Function Procedure?

So the main difference is that a Function Procedure can return a value, while a Sub can not.

So that’s the summary of what i learned about Sub and Function Procedure… The goal is to learn in more detail and in practice!

Follow me on Instagram @stem.spo .

Leave a Reply

Your email address will not be published. Required fields are marked *