Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 14th, 2011, 12:23 PM
Euskadi Euskadi is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2011
Posts: 9 Euskadi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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)

Reply With Quote
  #2  
Old November 17th, 2011, 11:37 AM
medialint's Avatar
medialint medialint is offline
Type Cast Exception
Dev Shed God 20th Plane (14500 - 14999 posts)
 
Join Date: Apr 2004
Location: OAKLAND CA | Adam's Point (Fairyland)
Posts: 14,939 medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)  Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 6 Months 2 Weeks 2 Days 1 h 38 m 6 sec
Reputation Power: 8490
Facebook
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

Reply With Quote
  #3  
Old November 17th, 2011, 11:41 AM
medialint's Avatar
medialint medialint is offline
Type Cast Exception
Dev Shed God 20th Plane (14500 - 14999 posts)
 
Join Date: Apr 2004
Location: OAKLAND CA | Adam's Point (Fairyland)
Posts: 14,939 medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)medialint User rank is General 112nd Grade (Above 100000 Reputation Level)  Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1Folding Points: 319635 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 6 Months 2 Weeks 2 Days 1 h 38 m 6 sec
Reputation Power: 8490
Facebook
One note, I didn't review your code before but I believe in your code you'll want

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


instead to be

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 ..

Reply With Quote
  #4  
Old December 7th, 2011, 05:48 PM
the-listener the-listener is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2011
Posts: 23 the-listener User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > Having real trouble understanding user-defiend functions

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap