# Solved: Visual Basic 2008 - Function Procedure



## witakr (Dec 9, 2006)

Hell yall!

I really need help!

I am writing this "simple" program for class.

I have to make it take the data(Double) that's entered by the user and calculate a bill based off the info entered.

here is the code i have so far:


```
Public Class frmBill

    Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
        'sets as Double for use in Call Statement Args
        Dim pq, fq, dq, result As Double
        'sets the cost amounts and stores them for later use
        Dim pc As Double = 1.75, fc As Double = 2.0, dc As Double = 1.25
        Dim o As String = "{0, -15}{1, 10}{2, 10}"
        pq = CDbl(txtPizza.Text)
        fq = CDbl(txtFries.Text)
        dq = CDbl(txtDrink.Text)
        lstResult.Items.Add(String.Format(o, "Item", "Quantity", "Price"))
        lstResult.Items.Add(String.Format(o, " ", " ", " "))
        calctotals("Pizza Slice", pq, pc)
        calctotals("French Fries", fq, fc)
        calctotals("Soft Drinks", dq, dc)
        lstResult.Items.Add(String.Format(o, " ", " ", " "))
        lstResult.Items.Add(String.Format(o, "Total:", " ", FormatCurrency(result)))'here is where i need to have the total, or result displayed
    End Sub
    Function calctotals(ByVal item As String, ByVal q As Double, ByVal c As Double) As String
        Dim result As Double
        Dim o As String = "{0, -15}{1, 10}{2, 10}"
        Dim out As String
        result = q * c 'multiplies the quantity of items by the cost of each item
        out = lstResult.Items.Add(String.Format(o, item, q, FormatCurrency(c)))
        Return out
    End Function
End Class
```
Now be gentle with your scrutiny, im new to VB coding.. its a LOT different from PHP...lol

WHat I need is for the result that is found in the function "calctotals" to be used in the last line of the "btnCalc_Click" sub.

I attached a screenshot of the gui incase you need it

please tell me if you dont understand or if you need more info


----------



## DoubleHelix (Dec 10, 2004)

Does your class require using proper naming conventions? A function returns a value. It doesn't make sense to write a function to return a string when it performs a mathematical operation.


----------



## witakr (Dec 9, 2006)

I know but they want us to use a function because we are learning functions right now... I thought it easier not to use the function but its required for the assignment


----------



## DoubleHelix (Dec 10, 2004)

I didn't say not to use a function. Use one that makes sense. The function performs math. Have it return a number. The function should not modify any other values or the main form. That functionality should be performed by the button click procedure.

And check into standard naming conventions. I wouldn't want to read code with variable names like "fq" and "o".


----------



## witakr (Dec 9, 2006)

oh,

I'm sorry I misread your reply. No he doesn't have any specific naming requirements. All he cares is if it works. He said he can handle reading the code regardless.

Perhaps I could alter my naming habits to suit other readers. This is a method I'm accustomed to and is easy for me to read, write and understand.

So I should use something like this instead...


```
Function calctotals(ByVal result As Double, ByVal quantity As Double, ByVal cost As Double) As Double
            Return result = quantity * cost 
End Function
```
...and perform the formatting either in the line above or in another sub procedure?


----------



## DoubleHelix (Dec 10, 2004)

I've always used Hungarian Notation. The variables would be dblCost and intQuantity.


----------



## witakr (Dec 9, 2006)

ok i managed to figure it out.

here is my revised code for those who need the help


```
Public Class frmBill
    Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
        lstResult.Items.Clear()
        forOut()
    End Sub

    Sub forOut()
        Dim pq, fq, dq, result As Double
        Dim pc As Double = 1.75, fc As Double = 2.0, dc As Double = 1.25 '------- pc = pizza cost, fc = fries cost, dc = drink cost
        pq = CDbl(txtPizza.Text) '----------------------------------------------- pq = pizza quantity 
        fq = CDbl(txtFries.Text) '----------------------------------------------- fq = fries quantity 
        dq = CDbl(txtDrink.Text) '----------------------------------------------- dq = drink quantity
        result = calcTotals(pq, pc) + calcTotals(fq, fc) + calcTotals(dq, dc) '-- adds the results of calcTotals together to get the bill total
        Dim o As String = "{0, -15}{1, 10}{2, 10}" '----------------------------- o = is just a placeholder for the string format
        lstResult.Items.Add(String.Format(o, "Item", "Quantity", "Price"))
        lstResult.Items.Add(String.Format(o, " ", " ", " "))
        lstResult.Items.Add(String.Format(o, "Pizzas Slices:", pq, FormatCurrency(pc)))
        lstResult.Items.Add(String.Format(o, "French Fried:", fq, FormatCurrency(fc)))
        lstResult.Items.Add(String.Format(o, "Soft Drinks:", dq, FormatCurrency(dc)))
        lstResult.Items.Add(String.Format(o, " ", " ", " "))
        lstResult.Items.Add(String.Format(o, "Total:", " ", FormatCurrency(result)))
    End Sub

    Function calcTotals(ByVal q As Double, ByVal c As Double) As Double
        Return q * c  '---------------------------------------------------------- Multiplies the Cost(c) of each item by the Quantity(q) of each item.
    End Function
End Class
```
EDIT: I forgot to add that in case you are wondering, I used the extra sub procedure because that is what he wanted us to do, we needed to make a program that calculated a bill in a restaurant and did the arithmetic in a function and formatted the output in a separate sub procedure.


----------

