Really stupid question: Passing values

Rype69
12-22-2002, 06:14 AM
Hi people,

Please can someone explain how to pass values/variables between subs. How do subs return values and what's the syntax?

Many thanx,

Rype69, UK.

JimCamel
12-22-2002, 06:29 AM
Hi,
Values and "Variables" are passed to subs/functions by just using their names after the call to the sub/function itself, seperated by comma's. It can either be in parenthesis or without (although sometimes this will slightly change how the call is made).

An example would be
ThisSub SomeInt, SomeStr, SomeLong
Here we are calling the Sub\Function ThisSub, and we are passing it the variables SomeInt, SomeStr and SomeLong.

Variables can be passed By Value or By Reference. By value means that the actual value is copied and passed to the sub/function, so the sub/function can make whatever changes they want to this value and there will be no change to the original variables value. Passing a variable by Reference however passes a pointer to where that variable is in memory. This means that when we modify this value, it will be saved over the variables old value.

Let us make an example sub "ThisSub"
Public Sub ThisSub(byVal anInt as integer, byRef aString as String, byRef aLong as long)
anInt = anInt + 5
aString = "Hello!"
aLong = aLong - 10
End Sub
Now lets say the values of anInt, aString and aLong are 10, "How are you?" and 15 respectively. anInt is passed byValue, while aString and aLong are passed by Reference. that means that right before we exit the sub, we have the values
anInt = 15 , aString = "Hello!" , aLong = 5
This is because anInt had 5 added to it's value, aString was changed to read "Hello!" and aLong had 10 removed from it's value. However, when we return to the function which called "ThisSub", we have the values:
SomeInt= 10 , SomeStr= "Hello!" , SomeLong= 5
Because SomeInt was passed by value, another copy was made, which "ThisSub" modified. This value was then lost. However, as SomeStr and SomeLong were passed by Reference, "ThisSub" Changed the original variables value, and so these will be stored.

Passing by reference is one such way of returning values. The other more common way is to use a Typed Function (for lack of a better word). Say we want a function which adds 2 numbers and divides by 2 (essentially taking the average of 2 numbers).
Public Function AverageTwo(byval Num1 as integer, byVal Num2 as integer) as integer
As you can see, this function takes two integers, "Num1" and "Num2". But at the end of the declaration it has an "as integer". This is the return type of the function. Lets take a look at the body of the function
Public Function AverageTwo(byval Num1 as integer, byVal Num2 as integer) as integer
AverageTwo = (Num1+Num2)/2
End Function
We can see that we are assigning a value to the name of the function, just as we would any variable. To use this we just have to have a variable to store it, thusly
averageResult = AverageTwo(FirstNumber, SecondNumber)
Then the variable averageResult will contain the result of the Function AverageTwo.

Hope this has been of some help.
Jim

Rype69
12-22-2002, 06:45 AM
Thanks Jim, you're a star.

Rype69
12-22-2002, 07:46 AM
Can a function output an array? For example, if the array is of strings, would the following function header be correct?

Private Function MatrixBFromFile() As String

JimCamel
12-23-2002, 04:15 AM
Yes they can, the definition is
Private Function MatrixBFromFile() As String()
The Parenthesis after the "String" indicate that it's an array.

anhmytran
12-23-2002, 04:47 AM
Return value from function should be Variant.
Here is the sample code:

Private Function GetArr(sIn As String) As Variant
' This function puts a string into an array of strings.
Dim i As Integer, iMax As Integer, myArr() As String
iMax = Len(sIn) - 1
ReDim myArr(iMax) As String
For i = 1 To iMax + 1
myArr(i - 1) = Mid$(sIn, i, 1)
Next i
GetArr = myArr
End Function

Garmour
12-23-2002, 05:02 AM
I thought variant was used ONLY if you didn't know (or care) what the return value was.

If you know it's an array, then return an array

Machaira
12-23-2002, 05:58 AM
You're correct garmour. Never use a Variant when you know the data type. JimCamel's declaration is the preferred one.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum