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
|
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
if b="dad" then MyFirstFunction="hello dad, my number " & c
end function
a simple function - ihope showing all the bits that count...
or does it
bill stewart - here to listen