Reputation: 11
Create a function called swapped that will take two integers(a,b) called byref and return a Boolean that is true if the numbers have been swapped. Within the routine compare the numbers and if b>a swap them using a local variable temp, then return the value true else return the value false.
Function swapped(ByRef a As Boolean, ByRef b As Boolean)
a = True
b = True
If b > a Then
temp = b
b = a
a = temp
a = True
b = True
Else
a = False
b = False
End If
Upvotes: 0
Views: 80
Reputation: 15774
Both existing answers do the trick. Here is one without a temp variable
Private Function swapped(ByRef a As Integer, ByRef b As Integer) As Boolean
If b > a Then
a = a + b
b = a - b
a = a - b
Return True
End If
Return False
End Function
Sub Main()
Dim a = 10
Dim b = 11
Console.WriteLine($"a: {a}, b: {b}")
Console.WriteLine($"values were{If(swapped(a, b), " ", " not ")}swapped.")
Console.WriteLine($"a: {a}, b: {b}")
Console.ReadLine()
End Sub
Upvotes: 0
Reputation: 86
Here is a fixed example. You should have the parameters declared as integers instead of boolean. The function itself should be declared as a boolean
Function swapped(ByRef a As Integer, ByRef b As Integer) As Boolean
If b > a Then
'Declare the temp variable
Dim temp As Integer = b
'Change b to a
b = a
'Set 'a' equal to the temp variable from the original b
a = temp
Return True
Else
Return False
End If
End Function
Public Sub test()
Dim intA As Integer
Dim intB As Integer
intA = 1
intB = 2
'Test b>a
'Should return True
Dim check As Boolean
check = swapped(intA, intB)
'Test Not(b>a)
'Should return False
Dim checkfalse As Boolean
checkfalse = swapped(2, 2)
End Sub
Upvotes: 1
Reputation: 15091
First let's look at your code.
'The requirements called for 2 Integers, not Booleans.
'A Function requires a datatype, in this can a Boolean
Private Function swapped(ByRef a As Boolean, ByRef b As Boolean)
'Assigning values here will overwrite the passed in values
a = True
b = True
'What would it mean for True > True ??
If b > a Then
'temp isn't declared so this will not compile
temp = b
'here I think you are trying to do the swap
'but all the values are True
b = a
a = temp
a = True
b = True
Else
a = False
b = False
End If
'Function need a return value to match the datatype of the function
'It is preceded by Return
End Function
If you are working in Visual Studio (free for the download), several of these errors will be highlighted for you.
Now, let's look at some code that may accomplish your task.
'Through comments you have already figured out that a and b are Integers
'In addition, Functions always need a return type. In this case a Boolean
Function swapped(ByRef a As Integer, ByRef b As Integer) As Boolean
'The values of a and b are passed to the function.
'Keep the passed in value of a in a new variable
If b > a Then
Dim aPassedInValue = a
'assign the value of b to a
a = b
'now assign the original value of a to b
'we can't use a because we just changed the value of a in the above line
b = aPassedInValue
'There have been no errors so we can return True
Return True
Else 'We didn't swap them
Return False
End If
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim c As Integer = 7
Dim d As Integer = 20
MessageBox.Show($"c = {c}, d = {d}", "Before Swap")
Dim ReturnValue = swapped(c, d)
MessageBox.Show($"The return value of the swapped Function is {ReturnValue}")
MessageBox.Show($"c = {c}, d = {d}", "After Swap")
End Sub
I think the point of this exercise is to demonstrate ByRef. You have changed the values in the function and the new values are reflected in the variable in the Button.Click Sub.
Upvotes: 1