|
-
Dec 3rd, 2002, 03:30 AM
#1
Thread Starter
New Member
Want to know different between Sub and Function
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
-
Dec 3rd, 2002, 03:47 AM
#2
Hyperactive Member
A function returns a value
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
Last edited by onerrorgoto; Dec 3rd, 2002 at 03:54 AM.
Onerrorgoto
Dont be to optimistic, the light at the end of the tunnel might be a train
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|