11-24-2023, 07:02 PM
(This post was last modified: 11-24-2023, 07:04 PM by Kernelpanic.)
Quote:@mnrvovrfc - First it prints 4 then 8. The "test" variable inside the subprogram is "protected", of type INTEGER which is two bytes. Although there is a variable named just like it which is supposed to be accessible inside that subprogram which is twice as large.Something is unclear there.
Without STATIC declaration line the second PRINT test would give 10.
Call by reference:
Code: (Select All)
'A little problem - 24. Nov. 2023
Option _Explicit
Declare Sub callthisthing (u As Long) As Long
Dim Shared test As Long
test = 4
Print " test 1 = "; test ' = 4
'Call by reference (Standard)
callthisthing test '= 1 -- First call!
'Because the procedure now receives the actual address
'of the variable and can change its value.
'The name in the procedure doesn't matter: u(test2) = 8
Print " test 2 = "; test ' = 8
Print
Print "End of my tests!"
End
Sub callthisthing (u As Long)
Static As Integer test
test = test + 1 ' = 1 -- (0 + 1)
Print " Sub test = "; test
u = u * 2
Print " Sub u = "; u ' = 8
End Sub
Call by value:
Code: (Select All)
'A little problem - 24. Nov. 2023
Option _Explicit
Declare Sub callthisthing (u As Long) As Long
Dim Shared test As Long
test = 4
Print " test 1 = "; test ' = 4
'Call by value
callthisthing (test) '= 1 -- First call!
Print " test 2 = "; test ' = 4 -- Because the procedure only worked with one copy.
Print
Print "End of my tests!"
End
Sub callthisthing (u As Long)
Static As Integer test
test = test + 1 ' = 1 -- (0 + 1)
Print " Sub test = "; test
u = u * 2
Print " Sub u = "; u ' = 8
End Sub