按值传递参数:
Module paramByval
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
Console.WriteLine("in swap, value of x : {0}", x)
Console.WriteLine("in swap, value of y : {0}", y)
End Sub
Sub Main()
' local variable definition
Dim a As Integer = 100
Dim b As Integer = 200
Console.WriteLine("Before swap, value of a : {0}", a)
Console.WriteLine("Before swap, value of b : {0}", b)
' calling a function to swap the values '
swap(a, b)
Console.WriteLine("After swap, value of a : {0}", a)
Console.WriteLine("After swap, value of b : {0}", b)
Console.ReadLine()
End Sub
End Module
Before swap, value of a :100 Before swap, value of b :200 in swap, value of x : 200 in swap, value of y : 100 After swap, value of a :100 After swap, value of b :200
按引用传递参数:
Module paramByref Sub swap(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 Sub Main() ' local variable definition Dim a As Integer = 100 Dim b As Integer = 200 Console.WriteLine("Before swap, value of a : {0}", a) Console.WriteLine("Before swap, value of b : {0}", b) ' calling a function to swap the values ' swap(a, b) Console.WriteLine("After swap, value of a : {0}", a) Console.WriteLine("After swap, value of b : {0}", b) Console.ReadLine() End Sub End Module
Before swap, value of a : 100
Before swap, value of b : 200
in swap, value of x : 200
in swap, value of y : 100
After swap, value of a : 200
After swap, value of b : 100