# Function question

Page 1 of 1

## 5 Replies - 444 Views - Last Post: 04 November 2011 - 10:09 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=254308&amp;s=6a4987dca4ba7c978b1bafe206d86a27&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jaArch

Reputation: 3
• Posts: 217
• Joined: 23-March 10

# Function question

Posted 04 November 2011 - 09:43 AM

Am I able to return a value that belongs in more than one result?

I mean, I want to return a value that is based off of 2 calculations and I was wondering if this is correct:

```Function CalcParkingFees() As Decimal
Dim dblDays As Double
Dim dcmParkingTotal As Decimal
Dim dblParkingFees As Decimal
dcmParkingTotal = CDec(txtPark.Text)
dblDays = CDbl(txtDays.Text)

dblParkingFees = CDec(dcmParkingTotal * dblDays)
dblParkingFees = CDec(dblDays * dblPARKING_FEES)

Return dblParkingFees
End Function
```

Thanks!

Is This A Good Question/Topic? 0

## Replies To: Function question

### #2 Tryparse

Reputation: 80
• Posts: 193
• Joined: 20-April 10

## Re: Function question

Posted 04 November 2011 - 09:57 AM

So, wait, you're trying to do what? If you're trying to return 2 results (i.e. the results of both calculations) you're going about it the wrong way, as line 9 up there will overwrite the result of line 8. If you're trying to get a function to return 2 results, there's a couple different ways of doing that.

Method 1: Return an array (or some other collection, or a custom object)
```Function CalcParkingFees() As Decimal()
...
Dim dblParkingFees(1) As Decimal
...

dblParkingFees(0) = CDec(dcmParkingTotal * dblDays)
dblParkingFees(1) = CDec(dblDays * dblPARKING_FEES)

Return dblParkingFees
End Function
```

Method 2: Pass in a ref parameter to hold the 2nd value:
```Function CalcParkingFees(ByRef dblMoreFees As Decimal) As Decimal
Dim dblDays As Double
Dim dcmParkingTotal As Decimal
Dim dblParkingFees As Decimal
dcmParkingTotal = CDec(txtPark.Text)
dblDays = CDbl(txtDays.Text)

dblParkingFees = CDec(dcmParkingTotal * dblDays)
dblMoreFees = CDec(dblDays * dblPARKING_FEES)

Return dblParkingFees
End Function
```

### #3 Ryano121

• D.I.C Lover

Reputation: 1455
• Posts: 3,281
• Joined: 30-January 11

## Re: Function question

Posted 04 November 2011 - 09:58 AM

I'm not sure what you mean by return a value that is based off of 2 calculations.

In this case the whatever value of the dblParkingFees variable has when you return it is passed by to the caller. You can have multiple return statements statements depending on some condition, but in this general sense you can only return one specific value.

If you want to pass back multiple calculations then you may want to create an object with a couple of properties representing the calculation results, that can then be returned as a single variable.

### #4 jaArch

Reputation: 3
• Posts: 217
• Joined: 23-March 10

## Re: Function question

Posted 04 November 2011 - 10:05 AM

Thatnks guys! That really helped.

Am I doing it the correct way if I want to calculate the total based on the amount of days the user enters and based on the amount the user enters in the parking fees textbox?

### #5 Ryano121

• D.I.C Lover

Reputation: 1455
• Posts: 3,281
• Joined: 30-January 11

## Re: Function question

Posted 04 November 2011 - 10:07 AM

Yeah that implementation is fine to use.

You may however want to think about passing in the values of the textbox as a decimal as parameters instead of getting them from inside of the function. It just gives more flexibility that way.

### #6 jaArch

Reputation: 3
• Posts: 217
• Joined: 23-March 10

## Re: Function question

Posted 04 November 2011 - 10:09 AM

Will do...thanks!