Good morning,

Can someone help me to figure out how if I should used a Public Sub New or a Public Function. My codes are:

Public Function ClassAverage(ByVal Average As String)

    Dim Grades() As Decimal = {76.5D, 63.4D, 45.4D, 94.5D, 81.3D, 66.2D, 68.6D}
    Dim sum As Decimal = 0

    For intCounter As Integer = 0 To Grades.Length - 1
        sum += Grades(intCounter)
    Next

    Average = (sum / Grades.Length)

    Return Average

End Function

Recommended Answers

All 7 Replies

A function calculates something and returns a value, your function tires to calculate an average from a string? Pull the Grades array out of it.

commented: thanks for your help! +0

The Assignment question was to: place the student names and final marks into two one-demensional arrays. One array will be a string (names), the other will be real numbers (student final marks). then I must write this, then read each student name and final mark separately. Then calculate Class average, # of student with highest mark.

I have written and read each student names, and final marks with success, but is unable to do the array part of it. (in bold fonts)

Deegirl -- The Function or Subroutine conundrum is as old as programming itself. At the lowest level (i.e. assembly/machine code) there is little difference. Both are branches to someplace else in the code stream and both carry the address of the branch point along with them so that they may return to the place from which they were called. The difference is only in how you return your result. With Subroutines you need to pass a pointer as an argument and then use that to pass back values upon returning. A function returns the response for you and allows you to assign it to some variable or use it directly in a call to another routine. Usually, this is merely a convenience issue since the same results can be accomplished using either approach. In most cases, the compiler would generate the same assembly code anyway. Here's an example in C (I chose C because it demonstrates more clearly what is going on with the pointers). The int Index in the argument list is the same as ByVal Index. The SomeType * Ret is pass by Pointer (the VB default).

SomeType FunctionName(int Index); //Declare your Func
SubName(SomeType * Ret, int Index); //And Subroutine

SomeType Ans;
for (int i=0; i<100; i++){
    GetAnsBySub($Ans, i);  //Send pointer to Ans and index
    Ans=GetAnsByFunc(i);    //Just send index
   }

In the first case, (the Sub) you pass in 2 arguments. The Sub uses them to compute a result and pass it back using the address passed into the sub. This has the advantage that the Sub can do some complex manipulation of the data that is already in Ans, which the function type of call cannot do. In the second case, you pass in One argument, which the function uses to compute some result, which it passes back as the return argument. You needn't know how the compiler does this, just that it does so on your behalf. As I said before, usually this is a matter of coding convenience.
In the first case, (the Sub) you pass in 2 arguments. The Sub uses them to compute a result and pass it back using the address passed into the sub. This has the advantage that the Sub can do some complex manipulation of the data that is already in Ans, which the function type of call cannot do. In the second case, you pass in One argument, which the function uses to compute some result, which it passes back as the return argument. You needn't know how the compiler does this, just that it does so on your behalf. As I said before, usually this is a matter of coding convenience.

sorry, I don't have the knowledge to understand what you just said, this is my first programming course; that's why a two page homework has been stressing me out for over one week. Thanks anyways!

Deegirl-- I'm sorry that I over shot the target. Take the whole previous comment and reduce it to: "It's a matter of programming convenience and what works for your problem. If you need a simple result returned, use a function and return the result. Otherwise use a Subroutine. If you only do something once, use neither - just write the code in-line"

Deegirl -- Just re-read your original post and realized that the problem is somewhat differnet from what I thought. The Sub vs. Function threw me off. You've already gotten the arrays built (Names and Grades). You don't specify how to index the array in the dimension statement, I will assume that the VB default for indexing starts at 0, as in your sample code. So the first name in the list will be Names(0) and the first final grade will be Grades(0). Then, the code you've written is in principle correct, except that it doesn't define the function in a valid way.

First, you specify an argument (byval Average as String) that you never use and whose declaration conflicts with the Local variable Average. Second, you didn't define a return type at all. Thus, your function Definition should be something like:

Public Function ClassAverage()as Double

You could also use the variable that VB implicitly provides in every function definition (aka the name of the function):

ClassAverage=0
loop--
    ClassAverage +=...
...
return

Then you would also be free to use another variable "Average" without conflict.

Finally, why bother making this a function at all unless you are writing a program to do many class averages from a large list? Then you should pass in the Grades array and return the average.
Otherwise, just put the code right in the main program.

Public Function ClassAverage()
        Dim the_class_average As Decimal = 0
        Dim Grades() As Decimal = {76.5D, 63.4D, 45.4D, 94.5D, 81.3D, 66.2D, 68.6D}
        Dim sum As Decimal = 0

        For intCounter As Integer = 0 To Grades.Length - 1
            sum += Grades(intCounter)
        Next

        the_class_average = (sum / Grades.Length)

        Return the_class_average
    End Function

Now your function will return the class average. Hope this helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.