Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
November 14th, 2011, 01:23 PM
Registered User

Join Date: Nov 2011
Posts: 9
Time spent in forums: 1 h 48 m 10 sec
Reputation Power: 0
Having real trouble understanding user-defiend functions

Say I have some code that populates an array with numeric values, and I want a function to calculate the average of these values.

I know how to create the code work out the average:
Code:
```Dim counter As Long = 0
Dim total As Double
Dim average As Double

For counter = 0 To UBound(myArray)
total = total + myArray(counter)
Next

average = total / (UBound(myArray) - 1)```

How can I implement this code into a UDF, so that I can just call it in my program as such
Code:
`calcAvg(myArray)`

#2
November 17th, 2011, 12:37 PM
 medialint
Type Cast Exception

Join Date: Apr 2004
Location: OAKLAND CA | Adam's Point (Fairyland)
Posts: 14,948
Time spent in forums: 6 Months 2 Weeks 2 Days 5 h 43 m 32 sec
Reputation Power: 8520
This might need a little tweaking depending on your platform but this is the way it typically works in old VB(5,6)/VBA

Code:
```Function calcAvg(ByRef MyArray() As Double) As Double
Dim counter As Long
Dim total As Double
Dim average As Double

For counter = 0 To UBound(MyArray)
total = total + MyArray(counter)
Next
average = total / (UBound(MyArray) - 1)
calcAvg = average ' << assign the return value to the function name to return the value
End Function

Sub TestCalcAvg()
Dim TestArray(10) As Double
Dim AverageResult As Double
Dim n As Integer
Randomize
For n = 0 To 9
TestArray(n) = Rnd * 40000
Next n
AverageResult = calcAvg(TestArray)
MsgBox AverageResult
End Sub```

I pass the array by reference (so it doesn't need to pass the whole array, just the pointer to it) to the function and the function returns the single average value. Note that to return the value from the function you assign the value back to the function name as I did in the last line I added to the end of your existing code when I wrapped it in a function.
__________________
medialint.com

“Today you are You, that is truer than true. There is no one alive who is Youer than You.” - Dr. Seuss

#3
November 17th, 2011, 12:41 PM
 medialint
Type Cast Exception

Join Date: Apr 2004
Location: OAKLAND CA | Adam's Point (Fairyland)
Posts: 14,948
Time spent in forums: 6 Months 2 Weeks 2 Days 5 h 43 m 32 sec
Reputation Power: 8520
One note, I didn't review your code before but I believe in your code you'll want

Code:
`average = total / (UBound(MyArray) - 1)`

Code:
`average = total / (UBound(MyArray) + 1)`

If the array is zero bound, then 0, 1, 2, 3 your UBound is 3 but there are four values, not two ..

#4
December 7th, 2011, 06:48 PM
 the-listener
Registered User

Join Date: Dec 2011
Posts: 23
Time spent in forums: 4 h 48 m 40 sec
Reputation Power: 0
the structure of the function

Quote:
 Originally Posted by Euskadi Say I have some code that populates an array with numeric values, and I want a function to calculate the average of these values. I know how to create the code work out the average: Code: ```Dim counter As Long = 0 Dim total As Double Dim average As Double For counter = 0 To UBound(myArray) total = total + myArray(counter) Next average = total / (UBound(myArray) - 1)``` How can I implement this code into a UDF, so that I can just call it in my program as such Code: `calcAvg(myArray)`

If you look at media lints example you will see a standard function

the parts are

the declaration "its a function" Function followed by A unique-simple-meaningful-name "myfirstfunction" followed by a pair of brackets and a type declaration "as string"

Function MyFirstFunction()as string

note not a subroutine "sub" a function "function"

this function takes no arguments and returns a string so you must pass the string back some how, this is done by using the functions name as a variable and assignimng the appropriate value to it before ending the function

so...
MyFirstFunction="Ah Ha!"
end function

in some languages you return the value of the function with a return statement that also closes the function in that case the last line might be...

return "Ah Ha!"

and there is no more

so my firstfunction looks like this

function MyFirstFunction()as string
MyFirstFunction="Ah Ha!"
end function

when you call it it returns the string "Ah Ha!"

I mentioned arguments these live in the brackets and are separated by comas they can be referenced by value or by looking up their value, i.e. you can pass a value or a reference to the function and it will be acted on and may change the variable passed in or may leave the variable as was and only work with its value at the time of entry... play about with byref and byval to fully appreciate the effect. the arguments will also have their types specified. you do not need to do any this and just put a varable in the brackets, it will be variant and take up more room than you really nead or be incompatable at somelevel, so it is suggetsted you dont do it , but we all do from time to time.

so function MFF(a)as string

i could pass the number 6 in and act upon it
e.g. for cnt=1 to a:doevents:next cnt

if passed byval ... function MFF(byval A) as string

a=a+1 would have no effect on the outside a

if passed byref ... function MFF(byref a) as string

a=a+1 would change the outside a imediately an dthat change would persist after the function had finished

SO BE VERY careful!

you can introduce loca variables witin the function that are outwith the scope of the rest of the program and will evaporate when the function finishes.

you simly declare them at the poit of use or more correctly before at the head of the function.

if you need to have a value persist you must pass it back or create a variable instance that will persist.. the latter is frowned on.

Like wise the habit of having large numbers of variable that are referenced both within and without your functions an d subroutines.

and so...

Function MyFirstFunction (byval A Int,byref b string) as string
dim c as int
c=1
if a= 1 then MyFirstFunction="hello mum, my number " & c

end function

a simple function - ihope showing all the bits that count...
or does it

bill stewart - here to listen

 Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > Having real trouble understanding user-defiend functions