Is it common practice to pass results BACK THRU a sub or
a function just as you pass parameters TO the sub or function?
Probably a stupid question, but I've never done this before and
I've just come across it for the first time.
Would there be an advantage to doing it this way
(as opposed to assigning values to public variables)?
syamin 10-02-2002, 10:12 AM my prof: Its a good practice
students: To avoid mark deduction.
me: I think to it helps to make codes more reusable, much more organise, ..... and good practice. :)
DavidPinzon 10-02-2002, 10:16 AM Is a common practice, yeah, most of the programmers say is not good to have global(public) variables. Why, because somehow you could lose the control over them. Any function could assign a value to it in any moment.
However I think this just makes your code prettier, if your code is not complex or long or both, you won't have trouble using global variables. plus if your function returns more than one value, your code has to be more elaborated if you dont use globals
OK, thanks then - makes sence.
Just that I never came across it 'till now.
Never used it in my own code as I didn't know you could do this.
In the past when a function had to calculate more than one
value, I'd just assign the results to public variables.
Will change my practice...
jayceepoo 10-02-2002, 11:26 AM FYI, you can't return a value back from a sub, only a function.
reboot 10-02-2002, 11:31 AM You can't return a value from a sub, but you can certainly pass a value as a parameter...
OnErr0r 10-02-2002, 11:38 AM By Reference no less
grashopper 10-02-2002, 11:40 AM Even if you don't return values for any other reason you should consider passing back information about whether the code in the function/subroutine failed (erred) or passed for error handling purposes.
This isn't really a requirement but it sure can make error-handling nicer.
Well maybe I'm misunderstanding what I'm doing, but I think
the return value is coming back from the sub.
Please let me know if this is actually doing something else.
Private Sub Command1_Click()
Dim SendString As String
Dim ReturnString1 As String
Dim ReturnString2 As String
SendString = "abcdefg"
Call MySub(SendString, ReturnString1, ReturnString2)
Debug.Print ReturnString1
Debug.Print ReturnString2
End Sub
Private Sub MySub(a As String, b As String, c As String)
b = UCase(a)
For i = 1 To Len(a)
c = c & Mid(a, i, 1) & UCase(Mid(a, i, 1))
Next i
End Sub
robot313 10-02-2002, 12:10 PM Yes, you are passing the parameters by reference (which is default) and the sub is changing them.
So ...
I am passing by reference (pointer to address??) - the sub
assigns a value to the variable - the next time that variable
is read it contains the new calculated/assigned value.
This is a perfectly acceptable technique?
(Sorry if I sound like an idiot, but as I said, I have never come
across this before.)
OnErr0r 10-02-2002, 12:25 PM Correct. The change is made to the variable at the address, instead of a copy of the varible.
Note: ByVal is default in .net
Derek Stone 10-02-2002, 12:59 PM Passing a pointer also takes a bit less time than passing the actual value when it comes to larger variables. For example, if you're passing a string that's 1MB in size you may very well benefit by passing it ByRef. I never do this myself, but it's worth mentioning nonetheless.
OnErr0r 10-02-2002, 02:52 PM As long as there is no COM marshaling for the string, that is true. If you're making an out-of-process component (AX EXE for example) you'll find its faster to pass strings ByVal.
In an EXE, even for small strings I always pass ByRef (and arrays of course), pretty much everything else is passed ByVal.
|