|
-
Jun 27th, 2001, 02:10 PM
#1
Thread Starter
Addicted Member
When to use a function or sub?
I know a the difference between a function and a sub, but when do I use function instead of a sub is confusing...
Best Regards!
Eric
-
Jun 27th, 2001, 02:13 PM
#2
just use a function if you want to return a value. if you just want a bunch of code to do stuff, use a sub.
-
Jun 27th, 2001, 02:14 PM
#3
Frenzied Member
You use a function when you need to return a value. If you don't need anything returned, use a sub.
-
Jun 27th, 2001, 03:48 PM
#4
Thread Starter
Addicted Member
or if i only use function. What will happen then?
-
Jun 27th, 2001, 07:37 PM
#5
Registered User
The difference between a subroutine and a function is that the
name of a subroutine is a way for the subroutine to be identified and called.
For a function, its name doubles as a variable declaration
that may be returned to the calling program.
If you use a function in the following way
VB Code:
Private Sub Command1_Click()
Dim a As Double
a = test
MsgBox a
End Sub
Function test() As Double
MsgBox "hgi"
End Function
The function executes the msgbox command and returns a double.
-
Jun 27th, 2001, 07:54 PM
#6
Registered User
My point was that a function returns a value even if you treat it like a sub and don't assign a return value, in response to:
or if i only use function. What will happen then?
My interpretation was he was asking what would happen if you only ever used functions and not subs.
I was trying to illustrate (with code rather than words) that even when you use a function like a sub and don't explicitly assign a retrun value, it still returns a value.
I knew what I was saying
Last edited by Nucleus; Jun 27th, 2001 at 07:57 PM.
-
Jun 27th, 2001, 08:22 PM
#7
Registered User
LOL.
I hate writing. Watch how scarce I become if I ever hear the words: help files or documentation !
-
Jun 28th, 2001, 06:29 AM
#8
Thread Starter
Addicted Member
thanx for the help, but how would that affect the speed?
-
Jun 28th, 2001, 10:05 AM
#9
Registered User
As a guess, I would say function to do the same thing vs sub, then the sub would use a little less memory and perhaps run a simgde slower.
In reality it doesn't really matter as other parts of your code will be the performance bottleneck and not this issue.
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
|