Synopsis: An example of the usefulness of the function in programming.
There are many sources and videos on the internet discussing the function. The function should be a single purpose program action. If the intent is to return more than one result, consider using a class. One aspect of a function is reuse of a simple process. In the following example, the software provides the functions reference count within the project, but not within the solution.

Actual Function Call = References (30) – Term Count in the Function (4) = 26 Calls. The function itself does not appear to be counted.
This may not seem to be a high count for an industrial program, but other functions handle other data types: GetDataRowBoolean, GetDataRowDouble, GetDataRowInteger, GetDataRowDate. The GetDataRow… functions work with data types from the database to return an expected value and to handle the NULL values which otherwise would be errors at function call.
Explanation of function GetDataRowString
- The function is used with a specific set of values passed in by the function call.
- If the Data is a database NULL or Nothing, then the function will take in the desired default value.
- String columns may be defined as 100 characters. If the string data is less than 100 characters, then the remainder of the string is filled with spaces.
- if the data (ElseIf) is “null” (possibly lowercase) which is different than “NULL” (note uppercase) then the default is returned.
- If the data “N/A” (uppercase or lowercase) the function returns the preferred default value.
- Finally (Else) if there is relevant data, the string is reviewed for blanks (Trim) on the left and right of the string and control characters are handled.
A function can return single data types. My most common are Boolean, String, Integer, and Date. Most functions have a default value and, if utilizing a database, will also handle NULL values.
In the real world, a Boolean generally is of bistate, which is of two values: True (1) or False (0). In the virtual world, a Boolean is actually a tri-state value of NULL, True (1), or False (0). The three states require addressing in programming languages because a database column starts with NULL unless otherwise specified in the column definition.
Example of a simple function call. The variable x has been defined as in integer and the function will be expected to return a square result.
The code could have been easily written as Textbox1.Text = 2* 2

If the program was required to return any value to any exponential power, the program would need to be written differently.
The function getValueSquared is basic but still useful and typically will not be involved in a function. However, the function getValueRaised would have extended usefulness in a program where various exponentiation applied against various values are needed. Programming should be useful to the purpose and expected to advance as the need comes up.
Private or Public: Private and Dim are applied to variables, functions, and subroutines when used locally in the form. Public is applied where the variables, functions, and subroutines will be used on other forms that, as showed above, Form1.
Title: All functions and subroutines are provided a title. The title should reflect intent of the programming.
Passed in information will need to be observed as all the two examples above are integers. To make the function even more useful, the function will need to be developed with non-integer values in mind. If a double/float is provided, the values will be trunked to integers.
If non-integer exponentiation is needed, using the built-in math functions should be used. The effort to work out a function solution is a bit much.
The next step is to have the data entered on the user form. Note the program is only going to use integer values for the exponent.
Enabling data entry on the form also requires enhancing the program to review data entry. Recall the program only accepts numeric values and integer exponents.
Unacceptable values will require screening to prevent the program from crashing or providing invalid results.



Expanding the visuals, the next step is to format the result.
Initial reporting to the RichTextBox was just running the calculation and displaying the result, GetValueRaised.
RichTextBox1.AppendText(myValue & "^" & myExponent & " = " & GetValueRaised & Chr(13))
Adding formatting to the result provided a bit of clarity to the number. Note the result GetValueRaised was converted to a string and rounded by the string formatting, “#,###.##”, in USA display form.
x_Result = GetValueRaised.ToString("#,###.####")
RichTextBox1.AppendText(myValue & "^" & myExponent & " = " & x_Result & Chr(13))

The code getting to formatted results took a good-sized leap from the initial code block in Simple Program Execution. The Result box does not have the formatted answer. The options are to 1) keep the display as is, 2) format the result before passing to the textbox, or 3) change the function to return a string value.
- Data entry was provided.
- Data errors are considered
- Calculate button is disabled on data error.
- Textbox labels toggled color on data error.
- User determination on when to run calculation.
- Historical calculation review was included.
- Results are formatted.
Public Class Form1
' Define global variables for use on this form only
Dim x_Value As Double = 0.0
Dim x_Raised As Double = 0.0
Dim x_Result As String = ""
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Set up the form's title
Me.Text = "Integer Exponent Caluclator"
' The Calculate button will be disabled at progam initialization
Button1.Enabled = 0 ' Zero is the value representation of False
End Sub
Dim Color1 As Color = Color.Blue
Dim Color2 As Color = Color.Green
Dim myColorOptBoolean As Boolean = 0 ' Set to false on program initialization
Private Function GetValueRaised(ByVal myValue As Double, ByVal myExponent As Double) As Double
' The function receives the value from the calling code and the
' expected return variable will be an integer
' Set the function default value to 1
GetValueRaised = 1
' Within the function, the value is rasied to the passed in exponent value.
' The loop count is determined by the exponent passed in.
For i As Integer = 1 To myExponent
' The value is multiplied to itself myExponent times
GetValueRaised *= myValue
Next
' Update the richtextbox with successful operations
RichTextBox1.SelectionColor = If(myColorOptBoolean, Color1, Color2)
myColorOptBoolean = Not myColorOptBoolean ' Toggle the boolean after each use
x_Result = GetValueRaised.ToString("#,###.####")
RichTextBox1.AppendText(myValue & "^" & myExponent & " = " & x_Result & Chr(13))
RichTextBox1.ScrollToCaret()
' The function title does not need to be directly assigned to the return result.
' Return is commonly used .
Return GetValueRaised
End Function
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
' Review the passed in data
ReviewDataPassedIn(TextBox1, Label1, "Term")
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
' Review the passed in data
ReviewDataPassedIn(TextBox2, Label2, "Exponent")
End Sub
Dim TermDataValidBoolean As Boolean = 0 ' Zero is the value representation of False
Dim ExpoDataValidBoolean As Boolean = 0 ' Zero is the value representation of False
Private Sub ReviewDataPassedIn(ByVal myTB As TextBox, ByVal myLabel As Label, ByVal myCase As String)
' Clear the result textbox (Textbox3) if there is typing in Textbox1 or Textbox2
TextBox3.Text = ""
' Use a Case to review the
Select Case myCase
Case "Term" ' Handles the Term Option
' Zero is the value representation of False
' One is the value representation of True
' This case is not going to be concerened on the type of Real Number passed in.
TermDataValidBoolean = If(IsNumeric(myTB.Text), 1, 0)
myTB.BackColor = If(TermDataValidBoolean, Color.White, Color.Red)
myTB.ForeColor = If(TermDataValidBoolean, Color.Black, Color.White)
' If the data is valid, then
If TermDataValidBoolean Then
' capture the term
x_Value = CDbl(myTB.Text)
End If
' If ther is an invalid entry, update the label
myLabel.Text = If(TermDataValidBoolean, "Value", "Numeric?")
myLabel.BackColor = If(TermDataValidBoolean, Color.Transparent, Color.Red)
myLabel.ForeColor = If(TermDataValidBoolean, Color.Black, Color.White)
Case Else ' Handles the Exponent Option
' Zero is the value representation of False
' One is the value representation of True
' This case is concerned on the type of value passed in.
ExpoDataValidBoolean = If(IsNumeric(myTB.Text), 1, 0)
' If the passed in value is numeric, then
Try ' try evaluating the data as if expecting an error
ExpoDataValidBoolean = If(CDbl(myTB.Text) = CInt(myTB.Text), 1, 0)
' If the data is valid, then
If ExpoDataValidBoolean Then
' capture the term
x_Raised = CDbl(myTB.Text)
End If
Catch ex As Exception
' And toggle the boolean to false
ExpoDataValidBoolean = 0
End Try
' If the try goes to error, possibly a string was entered.
myLabel.Text = If(ExpoDataValidBoolean, "Exponent", "Integer?")
myTB.BackColor = If(ExpoDataValidBoolean, Color.White, Color.Red)
myTB.ForeColor = If(ExpoDataValidBoolean, Color.Black, Color.White)
myLabel.BackColor = If(ExpoDataValidBoolean, Color.Transparent, Color.Red)
myLabel.ForeColor = If(ExpoDataValidBoolean, Color.Black, Color.White)
End Select
' If the entered values are valid, enable the execution button
Button1.Enabled = If(TermDataValidBoolean And ExpoDataValidBoolean, 1, 0)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox3.Text = GetValueRaised(x_Value, x_Raised)
End Sub
End Class