Hi, I just start learning VB, and one thing confuse me is the Sub and Function, what is different between them, when should I use Sub and when should I use Function or which one is better, are they both the same ?
Thank You
Printable View
Hi, I just start learning VB, and one thing confuse me is the Sub and Function, what is different between them, when should I use Sub and when should I use Function or which one is better, are they both the same ?
Thank You
in its simplest form:
A sub is used when you want a procedure that doesn't return any value.
A function returns a value after it has performed it's task.
Put a textbox nd a button on a form and type this:
VB Code:
Private Sub Command1_Click() Dim strRetVal As String 'call sub Call MySub(Text1.Text) 'call function strRetVal = MyFunction(Text1.Text) MsgBox strRetVal End Sub Public Sub MySub(strSubInput As String) Dim strTask As String strTask = "MySub " & strSubInput MsgBox strTask 'Show result End Sub Public Function MyFunction(StrFunctionInput As String) As String Dim strTask2 As String strTask2 = "MyFunction " & StrFunctionInput MyFunction = strTask2 'Return result to the calling procedure End Function