|
-
Oct 3rd, 2000, 11:10 PM
#1
Thread Starter
Member
Hi Programmers,
Let's discuss something.. one friend ask me this question.. and i seem i can't give a proper answer.
What's the Different Between a Sub and Function?
Thanks..
Cheers..!
Thomas Tan
-
Oct 3rd, 2000, 11:17 PM
#2
Hyperactive Member
Simple
A Function RETURNS something.
Statements are called to perform some process or routine (hence they are called a SUBroutine), while functions are used to go away and calculate values to RETURN.
Of course you can use a function "like" a subroutine in that what it returns is LOST but that is just VB.
Also note that if you don't specify a return type for the function it is automatically assumed to be a Variant return type.
-
Oct 4th, 2000, 01:26 AM
#3
Member
Function - Returns Value
Sub Routines - Does not return Values
sample:
dim intAns as integer
'This one is a function since something is returned
intAns = Msgbox("What Do You Want?",vbYesNo+vbExclamation,"Test")
'This one is a sub-routine
msgbox "Did you see the difference", vbQuestion + vbOK, "Test"
Also, as you can see, a function is called with the arguments enclosed in parenthesis but not in the sub-routine.
Well, that's just a supporting information to what Gen-X just posted. You can have a better and long explanation of this in the books.
Mikey
A/P
Using VB6 SP4 Enterprise Ed.
-
Oct 4th, 2000, 07:30 AM
#4
Some more clarifying code:
Code:
Option Explicit
Private Sub Form_Load()
Dim res As Long
' Calling the FUNCTION
Debug.Print FMultiply(10, 5)
' Calling the SUB
SMultiply 10, 5, res
Debug.Print res
' Callint the Second Sub
SMultiply2 10, 5
End Sub
' This Function Returns a long
Private Function FMultiply(ByVal a As Integer, ByVal b As Integer) As Long
FMultiply = a * b
End Function
' This Sub Doesn't return anything, BUT changes a variabele!
Private Sub SMultiply(ByVal a As Integer, ByVal b As Integer, ByRef result As Long)
result = a * b
End Sub
' This Sub Doesn't return anything
Private Sub SMultiply2(ByVal a As Integer, ByVal b As Integer)
Debug.Print a * b
End Sub
As you can see, it also matters if you pass a parameter ByVal or ByRef
For more info check out your helpfiles...
-
Oct 4th, 2000, 07:51 AM
#5
All of the answers are correct, as far as they go. Originally, there were only subroutines (Assembler). The command would cause the cpu to push the address of the current line, jump to the newline, and retrieve the saved address at the endsub command.
Functions were built-in to languages and were called similiarly to functions of current languages....except that you couldn't change them.
Currently, the actual physical difference is that subroutines don't return values and functions always return values. Conceptually, functions never change the program state, and subroutines always change the program state.
I was taught that if you passed a variable by reference (ie, sent the variable as an address) to a function, that you should immediately assign that reference to a local value to avoid any chance of contamination....program state change. Subroutines were not only expected to change the program state, they were REQUIRED to change the state.
Good Luck
DerFarm
-
Oct 4th, 2000, 07:56 AM
#6
Member
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
|