In: Computer Science
Im just having some trouble using functions in visual basics. Could you please explain what Im doing wrong? I think it has to do with the calling of the function.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As
EventArgs) Handles Button1.Click
Me.Close()
End Sub
Private Function Rsumation(ByVal intx As
Integer) As Double
intx =
CInt(Txtinput.Text)
If intx = 1 Then
Return 1
Else Return (intx +
Rsumation(intx - 1))
End If
End Function
Private Sub Button2_Click(sender As Object, e
As EventArgs) Handles Button2.Click
MessageBox.Show(CStr(Rsumation(CInt(Txtinput.Text)).ToString("N2")))
End Sub
End Class
Application Name :RsumationApp
Language Used :VB
User Interface :Form2.vb[Design]
Code :Form2.vb
Public Class Form2 'VB Class
'Button 1 click
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click
Me.Close() 'close form
End Sub
'Function
Private Function Rsumation(ByVal intx As Integer) As Double
'if intx is 1 then
If intx = 1 Then
Return 1 'return 1
Else
'return
Return (intx + Rsumation(intx - 1))
End If
End Function
'Button 2 click
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles
Button2.Click
'Call function and show the result in the messagebox
MessageBox.Show(CStr(Rsumation(CInt(Txtinput.Text)).ToString("N2")))
End Sub
End Class
=================================
Screen 1:
Screen 2:
Screen 3 :