It's good to check how functions receive data and send their results. We will take the code from the previous 05 example.
Code:
Public Class Form1Inside the "a1" function we can add this line:
Function a1(ByVal b1 As Integer, ByVal b2 As Integer) As Integer
Return b1 + b2
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "0"
TextBox2.Text = "0"
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.MouseHover
Label1.text = a1(TextBox1.Text, TextBox2.Text)
End Sub
End Class
MsgBox("b1 is = " & b1 & vbNewLine & "b2 is = " & b2 & vbNewLine & "b1 + b2 = " & b1 + b2)
& vbNewLine - allows us to proceed with our text to a new line in the message window
Code:
Public Class Form1Start the program. Insert this into textbox1: 20.40
Function a1(ByVal b1 As Integer, ByVal b2 As Integer) As Integer
MsgBox("b1 is = " & b1 & vbNewLine & "b2 is = " & b2 & vbNewLine & "b1 + b2 = " & b1 + b2)
Return b1 + b2
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "0"
TextBox2.Text = "0"
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.MouseHover
Label1.text = a1(TextBox1.Text, TextBox2.Text)
End Sub
End Class
Insert into textbox2: 10.30
Hove over label. The result should be: 30
Integer didn't calculate the decimal values in order for us to get: 30.70
Insert into textbox1: 20.50
Insert into textbox2: 10.51
The result should be: 31
Integer will round up numbers with a decimal value if their decimal value is above 0.50 up to 0.99
So, numbers with values after the decimal point are rounded up by the integer declaration.
(ByVal b1 As Integer, ByVal b2 As Integer) - integer will remove the decimal values and the new numbers will be used inside the "a1" function
That's why there wouldn't be changes if we did this:
Function a1(ByVal b1 As Integer, ByVal b2 As Integer) As Double
- the decimal value has been already removed at the beginning of the function, and we can't retrieve it anymore
Let's change this:
Function a1(ByVal b1 As Integer, ByVal b2 As Integer) As Integer
into this instead:
Function a1(ByVal b1 As Double, ByVal b2 As Double) As Integer
Code:
Public Class Form1Start the program.
Function a1(ByVal b1 As Double, ByVal b2 As Double) As Integer
MsgBox("b1 is = " & b1 & vbNewLine & "b2 is = " & b2 & vbNewLine & "b1 + b2 = " & b1 + b2)
Return b1 + b2
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "0"
TextBox2.Text = "0"
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.MouseHover
Label1.text = a1(TextBox1.Text, TextBox2.Text)
End Sub
End Class
Insert into textbox1: 20.30
Insert into textbox2: 10.20
Hover over the label. The result in the message box should be: 30.50, while the result received inside the label will be 30.
Insert into textbox1: 20.40
Insert into textbox2: 10.50
Message box result: 30.90
Result received by the label control: 31 - integer rounds it up again.
The data type declaration that we add - after declaring the variables that will be used inside the function - it's a data type that describes the returned value of a function.
So: Function a1(variables) As "Something"
"Something" is a data type that will describe the returned value of the function.
We can also remove the data type at the end. Change this line:
Function a1(ByVal b1 As Double, ByVal b2 As Double) As Integer
into this:
Function a1(ByVal b1 As Double, ByVal b2 As Double)
Now we will get out whatever data type the function creates at the end. We don't turn the data into a specific type.
Code:
Public Class Form1
Function a1(ByVal b1 As Double, ByVal b2 As Double)
MsgBox("b1 is = " & b1 & vbNewLine & "b2 is = " & b2 & vbNewLine & "b1 + b2 = " & b1 + b2)
Return b1 + b2
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "0"
TextBox2.Text = "0"
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.MouseHover
Label1.text = a1(TextBox1.Text, TextBox2.Text)
End Sub
End Class
There is a general habit of adding the "Private" modifier when creating functions and subs in visual basic. We limit the usage of the elements this way. Other parts of a person's program, like forms, won't be able to access the data inside the functions.
Here is the microsoft description of using "Private":
If a programming element represents proprietary functionality, or contains confidential data, you usually want to limit access to it as strictly as possible. You achieve the maximum limitation by allowing only the module, class, or structure that defines it to access it. To limit access to an element in this way, you can declare it with Private.
Resource page.