This document contains code for a form application that demonstrates call by value vs call by reference when passing parameters to subroutines. It includes subs to swap integer values using call by value and call by reference, as well as functions to calculate the factorial of a number and find the maximum of two numbers.
This document contains code for a form application that demonstrates call by value vs call by reference when passing parameters to subroutines. It includes subs to swap integer values using call by value and call by reference, as well as functions to calculate the factorial of a number and find the maximum of two numbers.
This document contains code for a form application that demonstrates call by value vs call by reference when passing parameters to subroutines. It includes subs to swap integer values using call by value and call by reference, as well as functions to calculate the factorial of a number and find the maximum of two numbers.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click no1 = TextBox1.Text no2 = TextBox2.Text swap(no1, no2) MsgBox(" After swap call by value no1=" & no1 & " No2=" & no2) swapref(no1, no2) MsgBox(" After swap call by reference no1=" & no1 & " No2=" & no2) End Sub Sub swap(ByVal x As Integer, ByVal y As Integer) Dim temp As Integer temp = x ' save the value of x x = y ' put y into x y = temp 'put temp into y
End Sub
Sub swapref(ByRef x As Integer, ByRef y As Integer)
Dim temp As Integer temp = x ' save the value of x x = y ' put y into x y = temp 'put temp into y End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles
Button4.Click no1 = TextBox1.Text TextBox2.Text = factorial(no1) End Sub Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click no1 = TextBox1.Text no2 = TextBox2.Text TextBox3.Text = MaxNo(no1, no2) End Sub Public Function MaxNo(ByVal a As Integer, ByVal b As Integer) As Integer If (a > b) Then Return a Else Return b End If End Function
Function factorial(ByVal m As Integer) As Integer
If m = 0 Then Return 1 Else Return m * factorial(m - 1) End If End Function End Class