Hey guys, i am making a small function to add up all the numbers between 2 numbers.
My code:
Whenever i call the function: tb = Tria(b, bk), my variable b gets changed in the process. Why is this happening?
Of course if i just do: Tria = (n * (n + k)) / 2 it all works, but i was under the impression that if my variable was dimmed in a separate sub and it should not be reachable? Or do functions work different?
My code:
Code:
Sub Sums() Dim a As Long, b As Long, i As Long, n As Long, ta As Long, tb As Long, ak As Long, bk As Long
a = 0
b = -2
ak = 1
bk = 1
If a - b = 0 Or a + b = 0 Then
n = 0
Else:
If a = b Then
n = a
Else:
If a < 0 Then ak = -1
If b < 0 Then bk = -1
ta = Tria(a, ak)
tb = Tria(b, bk)
End If
End If
If a > b Then
n = ta - tb
Else:
n = tb - ta
End If
Debug.Print n
End Sub
Function Tria(n As Long, k)
n = (n * (n + k)) / 2
Tria = n
End Function
Whenever i call the function: tb = Tria(b, bk), my variable b gets changed in the process. Why is this happening?
Of course if i just do: Tria = (n * (n + k)) / 2 it all works, but i was under the impression that if my variable was dimmed in a separate sub and it should not be reachable? Or do functions work different?
Last edited: