|
-
Jul 5th, 2000, 06:27 AM
#1
Thread Starter
Hyperactive Member
Can anyone tell me the difference between subs and functions, sub and functions which can receive values and all. Any kinda help will be greatly appreciated.
-
Jul 5th, 2000, 06:36 AM
#2
Hyperactive Member
A function can return a value (actually, it does return a value), a sub can't (doesn't).
-
Jul 5th, 2000, 07:40 AM
#3
Frenzied Member
Basicly a Sub is just a piece of code that runs when you call it. for Example a PrintCentral Sub could be as follows
Code:
Public Sub PrintCentral(str As String)
Form1.CurrentX = (Form1.ScaleWidth - Form1.TextWidth) / 2
Form1.CurrentY = (Form1.ScaleHeight - Form1.TextHeight(str)) / 2
Form1.Print str
End Sub
Which Prints Text into the Centre of your Form (Try it, not very usefull but Still)
But You Can't Get any information Back From The Sub, it just runs and does something.
If You Want to Get Information Back You Can Use a Function For example You Could Have a Function That Takes 2 Numbers and Gives You a Times table Type String, so 3 and 4 would give you "3 Times 4 is 12" Like This
Code:
Public Function TimesTables(FirstNumber As Integer, SecondNumber As Integer) As String
TimesTables = CStr(FirstNumber) & " Times " & _
CStr(SecondNumber & " Is " & _
CStr(FirstNumber * SecondNumber) & "."
End Function
Now All You Have to Do is this
PrintCenral TimesTables(3,4)
and your TimesTable Apears in the Centre of the Form.
Hope This Helps
-
Jul 5th, 2000, 05:26 PM
#4
Frenzied Member
Originally posted by Crazy D
A function can return a value (actually, it does return a value), a sub can't (doesn't).
Kinda depends on how you define "return". I've got subroutines that take a string, modify it and return the modified string to the calling routine.
-
Jul 5th, 2000, 05:58 PM
#5
Frenzied Member
Return is defined in programming as that thing functions do but subs don't (probably not the dictionary definition but it's what it means)
modifying byRef variables is Technicly a side efect of a function.
-
Jul 5th, 2000, 06:44 PM
#6
Frenzied Member
That may be your definition but it's not mine.
-
Jul 5th, 2000, 07:52 PM
#7
Frenzied Member
well it's what it means, I can't remember exactly how it's defined, but the value the function returs is the value of mVar after the line
mVar = MyFunc(params..)
It doesn't matter much as long as you can distinguish between the 2, but don't go talking about a return value when you mean an out parameter (which is what it was, I've just remembered, a side effect is something similar but not useful) it can confuse people.
-
Jul 6th, 2000, 01:49 AM
#8
Hyperactive Member
Yes of course a sub can return a value too by passing the params ByRef. But, a function can *always* return a function, no matter if the params are byval or byref.
-
Jul 6th, 2000, 08:34 AM
#9
Fanatic Member
My 2.1 cents
Subs are faster (by about a factor of ten) and Module subs are fastest!
But considering the time periods involved this is really a very small issue.
A sub is a the same a C/C++ Void function. ie it returns NO value.
Passing a referernce to a sub and having it modified is not "Returning" a value. Only one value can be returned by a function: a variable, array, UDT or object pointer
A sub "Returns" in the sense that it finishes and operation is passed back to the sub or function that called it but that's it.
References can be passed to functions too and can be modified but these values are not known as the return value.
CrazyD... Why does your first and last post argue opposite points? What DO you believe? or does it depend on luna cycles?
Paul Dwyer 
Network Engineer
Aussie In Tokyo
Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)
-
Jul 6th, 2000, 08:44 AM
#10
Lively Member
Poor wasiq-
Presumably your question is more than sufficiently answered!!
I think vb-world needs a "Pendantic Answers" forum... 
(Please don't flame-- it *was* meant in good humour!!)
-
Jul 6th, 2000, 09:04 AM
#11
Fanatic Member
-
Jul 6th, 2000, 09:09 AM
#12
-
Jul 6th, 2000, 11:57 AM
#13
New Member
On The Bandwagon
I'd just like to jump on the bandwagon and give you my two cents (Holy Mixed Metaphors Batman!!) I agree with Sam Finch in that functions RETURN a variable, subs do not. A sub can be PASSED a vlue, by reference, which changes the value of the variable during the execution of the sub. At the end of the sub NOTHING is really returned to where the sub was called. Functions return values, subs don't!
Jonathan (Co President - Big Berdie International)
Karl Moore RULEZ!
"Pipers Do It With Amazing Grace!"
-
Jul 6th, 2000, 12:05 PM
#14
Fanatic Member
Seeing as we are all offering opinions, here's mine.
Becuse a fucntion can return a value, it can be used in a Logic or Arithmetic operation. A sub cannot.
Iain, thats with an i by the way!
-
Jul 7th, 2000, 01:13 AM
#15
Thread Starter
Hyperactive Member
thanks for all ur help, i think its enough for my little question. I can fully differentiate between a sub and a function (hopefully, . thanks again
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
|