The most basic of all ASP.NET 3.5 server side scripts that I've covered using the Visual Basic programming language is not modular in nature. This means that an ASP.NET 3.5 server will interpret the scripts in the Visual Basic file (e.g Default.aspx.vb) from top to bottom. In most real-world applications that use Visual Basic in ASP.NET websites, however, most web developers structure their programs in “modules.” This article will give you information about subroutines and functions, along with practical examples and their advantages.
This is a two-part article series. The first article covers the theory of functions and subroutines, while part two implements these concepts with an example project.
Understanding the Modular Programming Concept
Imagine modules like “blocks” in programming scripts with the sole purpose of accomplishing certain tasks. When the script uses functions or subroutines, the program is said to be “modular.” A “module” is the block of scripts that accepts inputs and aims to perform specific tasks. A “call” is a line in the Visual Basic source code which will actually tell the modules to execute. If functions or subroutines are not called, they are never executed in the script or outputted to the browser.
The modular programming concept can simplify programs and reduce redundant code, making them easy to read, easy to troubleshoot and user-friendly. Below is the modular programming concept plan of the ASP.NET web application to compute the area of the circle.
The original ASP.NET web application project of the screen shot above (which did not use functions/subroutines) was discussed in this tutorial.
When functions/subroutines are employed, the developer will first write the function which can accept inputs (parameters). Somewhere in the program (such as in the last part), the function will be called to be executed and then return the output to the browser.
Difference between Subroutines and Functions
In what you read above, we used the terms "function" and subroutine" interchangeably. In fact, however, they are not the same. When “functions” are used in the script, they are used to return values. This is why you should commonly associate functions with blocks/modules of scripts that do computation and analysis to return some value. In the above example, you read of the use of function to compute the area of the circle, which will return the value (area) to the web browser.
Subroutines use a structure and syntax similar to functions. They can accept inputs (as parameters), but will not return values.
Subroutines are not used to compute or return values to the user; instead, they are used to modularize scripts to increase efficiency, reduce code redundancy and improve readability of the scripts.
The syntax of a subroutine using Visual Basic in ASP.NET 3.5 is shown below:
Sub Subroutinename(ByVal variable as variable type)
Subroutine_instructions_here
End Sub
To call the subroutine in the program, you will use the following syntax:
Subroutinename()
Let’s write a simple ASP.NET web application that will use a subroutine. Say for example the web application will accept your name (first name and last name) and then display it in the web browser.
Following the concepts to create a web form discussed previous, the web form should look like this:
For example, if you are using Default.aspx as the ASP.NET web form, the complete script/markup code of Default.aspx will be:
The Visual Basic server side script that will process the user inputs (first name and last name) will be placed in Default.aspx.vb. This is also where you will need to place the subroutine. You will need to create a click event handler for the submit button by double clicking the “Submit” button in the Design view using Visual Web Developer Express. We've discussed event handlers in ASP.NET previously; you can click on the link to review this topic.
Here is what the empty script looks like, after double clicking the submit button to create the click event handler:
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub
End Class
The subroutine will be added between Partial Class and End Class. The call script can be placed after the subroutine between Protected Sub and End Sub.
Suppose, then, that Showmyname is the name of the subroutine, and name is the variable used in the subroutine. In that case, the subroutine can be written like this:
Sub Showmyname(ByVal name As String)
displayname.Text = "Your name is: " & name
End Sub
The subroutine's job is to display the name in the browser. Since it are not yet called, the names are not displayed.
The main script (outside the subroutine) can be written like this:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim inputname As String = TextBox1.Text
Showmyname(inputname)
End Sub
Therefore the complete Default.aspx.vb script is as follows:
Partial Class _Default
Inherits System.Web.UI.Page
Sub Showmyname(ByVal name As String)
displayname.Text = "Your name is: " & name
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Now that you have a clear understanding of what a subroutine is and how it is used in ASP.NET, this section will discuss what functions can accomplish. As mentioned earlier, they use similar syntax, structures and calling procedures. The syntax for a function is as follows:
Function Functionname(Variablename as Variabletype) as Variabletype
Function_instructions_here
Return outputvariable
End Function
As we did in the previous section on subroutines, let us illustrate the application of functions using an example. Recall the ASP.NET web application to compute the area of the circle we covered in a previous tutorial.
Let us use functions to compute the area of the circle. First, you need to create a project in Visual Web Developer Express and then copy/paste the Default.aspx source code below:
<asp:Button ID="computearea" runat="server" Text="Compute Area of the Circle"
style="font-family: Verdana" />
<br />
<br />
<asp:Label ID="displayarea" runat="server"
Text="Results here after clicking submit button" style="font-family: Verdana"></asp:Label>
</div>
</form>
</body>
</html>
When viewed using Design View in Visual Web Developer Express, it should look like this:
Using the same procedure we would with a subroutine, you need to double click “Compute Area of the Circle” to create a click event handler and show the Default.aspx.vb empty script:
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub computearea_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles computearea.Click
End Sub
End Class
To create the function to compute the area of the circle, assume you will use the following variables:
ComputeAreaofCircle = name of the function
radius = function variable input
Using the defined syntax for a function, the area of a circle can be computed using the following code:
Function ComputeAreaofCircle(ByVal radius As Decimal) As Decimal
Dim AreaofCircle As Decimal
AreaofCircle = (3.1416) * ((radius) ^ 2)
Return AreaofCircle
End Function
Since Visual Basic is a strongly-typed language, both function names and all variables should have a declared type.
The main script (to be contained between Protected Sub and End Sub) will be used to call the function:
Protected Sub computearea_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles computearea.Click
Dim radius As Decimal = userradius.Text
displayarea.Text = "The area of the circle is: " & ComputeAreaofCircle(radius)
End Sub
As you can see, it is similar to the original code (which does not use functions), but in this case ComputeAreaofCircle(radius) has been used to return the values to the browser.
Here is the final Visual Script (Default.aspx.vb) for computing the area of the circle using Functions:
Partial Class _Default
Inherits System.Web.UI.Page
Function ComputeAreaofCircle(ByVal radius As Decimal) As Decimal
Dim AreaofCircle As Decimal
AreaofCircle = (3.1416) * ((radius) ^ 2)
Return AreaofCircle
End Function
Protected Sub computearea_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles computearea.Click
Dim radius As Decimal = userradius.Text
displayarea.Text = "The area of the circle is: " & ComputeAreaofCircle(radius)
End Sub
End Class
In the second part, we'll go over a case study in which functions and subroutines are used together.