Most of the code we write is in the form of custom functions or subroutines that are called from several places in the application. Subroutines are just like functions, except that they don’t return a value, so we’ll focus on the implementation of custom functions. With the exception of a function’s return value, everything else presented in this and the following section applies to subroutines as well.
Let’s look at an example of a fairly simple (but not trivial) function that does something really useful. tutorials are identified by a unique international standard tutorial number (ISBN), and every application that manages tutorials needs a function to verify the ISBN, which is made up of 12 digits followed by a check digit. To calculate the check digit, you multiply each of the 12 digits by a constant; the first digit is multiplied by 1, the second digit is multiplied by 3, the third digit by 1 again, and so on. The sum of these multiplications is then divided by 10, and we take the remainder. The check digit is this remainder subtracted from 10. To calculate the check digit for the ISBN 978078212283, compute the sum of the following products:
9 * 1 + 7 * 3 + 8 * 1 + 0 * 3 + 7 * 1 + 8 * 3 +
2 * 1 + 1 * 3 + 2 * 1 + 2 * 3 + 8 * 1 + 3 * 3 = 99
Code language: VB.NET (vbnet)
The sum is 99; when you divide it by 10, the remainder is 9. The check digit is 10 – 9, or 1, and the tutorial’s complete ISBN is 9780782122831. The ISBNCheckDigit() function, shown in Listing 3.9, accepts the 12 digits of the ISBN as an argument and returns the appropriate check digit.
Listing 3.9: The ISBNCheckDigit() Custom Function
Function ISBNCheckDigit(ByVal ISBN As String) As String
Dim i As Integer, chksum As Integer = 0
Dim factor As Integer = 3
For i = 0 To 11
factor = 4 - factor
chksum += factor * Convert.ToInt16(ISBN.Substring(i, 1))
Next
Return (((10 - (chksum Mod 10)) Mod 10)).ToString
End Function
Code language: VB.NET (vbnet)
The ISBNCheckDigit() function returns a string value because ISBNs are handled as strings, not numbers. (Leading zeros are important in an ISBN but are totally meaningless, and omitted, in a numeric value.) The Substring method of a String object extracts a number of characters from the string to which it’s applied. The first argument is the starting location in the string, and the second is the number of characters to be extracted. The expression ISBN.Substring(i, 1) extracts one character at a time from the ISBN string variable. During the first iteration of the loop, it extracts the first character; during the second iteration, it extracts the second character, and so on.
The extracted character is a numeric digit, which is multiplied by the factor variable value and the result is added to the chkSum variable. This variable is the checksum of the ISBN. After it has been calculated, we divide it by 10 and take its remainder (the first Mod operator returns the remainder of this division), which we subtract from 10. The second Mod operator maps the value 10 to 0. This is the ISBN’s check digit and the function’s return value.
You can use this function in an application that maintains a tutorial database to make sure that all tutorials are entered with a valid ISBN. You can also use it with a web application that allows viewers to request tutorials by their ISBN. The same code will work with two different applications, even when passed to other developers. Developers using your function don’t have to know how the check digit is calculated, just how to call the function and retrieve its result.
To test the ISBNCheckDigit() function, start a new project, place a button on the form, and enter the following statements in its Click event handler (or open the ISBN project in the folder with this chapter’s sample projects):
Private Sub Button1 Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Console.WriteLine("The check Digit is " &
ISBNCheckDigit("978078212283"))
End Sub
Code language: VB.NET (vbnet)
After inserting the code of the ISBNCheckDigit() function and the code that calls the function, your code editor should look like Figure 3.1. You can place a TextBox control on the form and pass the Text property of the control to the ISBNCheckDigit() function to calculate the check digit.
Figure 3.1 – Calling the ISBNCheck-Digit() function
A similar algorithm is used for calculating the check digit of credit cards: the Luhns algorithm. You can look it up on the Internet and write a custom function for validating credit card numbers.