1

I would like to create two functions in VB.NET, one of which will calculate the area, and the other function will get the total price by multiplying the area by the user's entered price. Is there a way that I can include the output of one function inside another? My program will not compile currently, as I believe my logic is off. Thanks!

Public Function area(ByRef enteredLength As Double, ByRef enteredWidth As Double)
    area = Val(enteredLength) * Val(enteredWidth)
End Function

Public Function totalPrice(ByRef enteredLength As Double, ByRef enteredWidth As Double)
    totalPrice = Val(area) * Val(enteredPrice)
End Function

2 Answers 2

1

You can call the function and use the returned value as part of the calculation, like this:

Public Function area(ByRef enteredLength As Double, ByRef enteredWidth As Double) As Double
    Return enteredLength * enteredWidth
End Function

Public Function totalPrice(ByRef enteredLength As Double, ByRef enteredWidth As Double) As Double
    Return area(enteredLength, enteredWidth) * enteredPrice
End Function

It is not necessary to use the Val() function, because you are multiplying Double values together and returning a Double, as shown in the code above (As Double).

Note: You can also use the Return keyword, as well as the name of the Function to return a value.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this. Need to pass the parameter values into the area function call inside totalPrice

Public Function area(ByRef enteredLength As Double, ByRef enteredWidth As Double)
    area = enteredLength * enteredWidth
End Function

Public Function totalPrice(ByRef enteredLength As Double, ByRef enteredWidth As Double)
    totalPrice = area(enteredLength, enteredWidth) * enteredPrice
End Function

I would also recommend changing your function parameters to ByVal instead of ByRef. I would consider passing in the "enteredPrice" value as a parameter to the totalPrice function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.