How to make a function return a value in visual basic?

1

Can someone tell me why this does not return value? I pass you

Public Class Form1

Dim peso As Integer
Dim altura As Integer
Dim imc As Integer

Private Sub Calcular_Click(sender As System.Object, e As System.EventArgs) Handles Calcular.Click
    peso = PesoBox.Text
    altura = AlturaBox.Text

    Resultado.Text = calcularimc(peso, altura, imc)

End Sub

Function calcularimc(ByVal peso As Integer, ByVal altura As Integer, ByVal imc As Integer) As Integer

    imc = peso / (altura) ^ 2

    Return imc
End Function


End Class
    
asked by Aritzbn 07.09.2017 в 11:42
source

1 answer

1

The problem is that your function returns a Integer . The calculation you do is probably worth a decimal between 0 and 1 , which when converted to Integer results in 0 .Af your function should remain:

Function calcularimc(ByVal peso As Integer, ByVal altura As Integer) As Double
    Dim imc As Double = peso / (altura) ^ 2
    Return imc
End Function

As you can see, I have also eliminated the imc parameter from the function, since it does not make sense. To call it, simply:

Resultado.Text = calcularimc(peso, altura)
    
answered by 07.09.2017 / 11:56
source