Visual Basic: What’s a subroutine / Difference between Subroutine and Function?

Visual Basic: What’s a subroutine / Difference between Subroutine and Function?

.NET framework

Subroutines are another word for methods, and they’re code blocks that will be executed by your application as you call them elsewhere in your applications. To call a subroutine, just call it by the subroutine’s name (for instance, if you created your own subroutines).

The Public, Private and Protected keywords are incase you create multiple classes (explained below) and you want to choose whether other classes have access to use the respective methods (or subroutines) you have created. You can search online for the exact definition of these three keywords.

Say you have a subroutine that does some task of some kind, you call it and the code within that subroutine gets executed. However, the thing is, what if you wanted to make sure some code in the subroutine did it’s job? What about using the Return keyword and return either true or false depending on whether the portions of the code block was executed? You may want to do this at some point in your Visual Basic development. Either way, subroutines cannot return values – they’re there purely to execute code. If you wanted to do this, you’d need to use a Function in Visual Basic. It’s like a Subroutine, except at some point you have to return a value of some form, like: Return TRUE etc.

Unlike Subroutines, with Functions you have to specify the data type of the Function (in other words, what sort of data type you’re returning. Here’s a basic example:

Public Function returnTrue() As String
Return "Executed"
End Function

The brackets are required there incase you may want to take in arguments as part of when some other part of the application call’s the function. For example:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
returnTrue("Hello world")
End Sub
Public Function returnTrue(ByVal n) As String
Return n
End Function

Ignore ByVal for now, you can learn that later – it’s just telling Visual Basic how to handle the argument (the variable in the brackets). You can always search online about the ByVal keyword. Now, all this does is returns whatever you enter when calling the Function. If you forget to add ByVal when creating a Function or Subroutine, Visual Studio or Visual Basic Express will do it for you – incase you forget. You can also specify arguments (brackets) for Subroutines too. The only difference is Subroutines cannot return values, hence why Functions require an As Type keyword after them.

What are classes and methods?

Methods are the code blocks that are programming functionality or logic (in other words, the code in a method code block (subroutine) does something – therefore it is programming functionality). In VB, all programming functionality is wrapped within a method (or what is called a subroutine or function). Classes are simply structures for all the methods you create (or VB create for you in the application), and is there purely for organisation of multiple methods. All Visual Basic applications and code must reside within a class and method of some kind – such as a subroutine or function. A lot of other programming languages have these sorts of features (classes, methods) and some also require that all code also be within a class and method. By the way, this method of coding (classes/methods/etc) is called object oriented programming. It’s a complicated word and you don’t need to worry about what the word means for now, but generally object oriented programming is a programming paradigm (way of programming) in languages like Visual Basic, Java, C++, etc.

Like this post ?

Share on your Social Networking Profile ( Facebook, Twitter & Google+ ) and get a flat 10% Recurring discount on our VPS Hosting and Dedicated Servers.

Email us the shared link at : [email protected] or speak to our live chat operator now, by clicking on the “Live Chat” Scroller on the left-hand side of this page and we will provide you with the discount Coupon right away!

Ben Stones

Sharing

Leave your comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.