Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A Question on Calls to Subroutines
#8
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.

Without STATIC declaration line the second PRINT test would give 10.
Something is unclear there.  Huh

Call by reference:
Code: (Select All)

'A little problem Wink - 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

[Image: Call-by-reference-2023-11-24-192312.jpg]

Call by value:
Code: (Select All)

'A little problem Wink - 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

[Image: Call-by-value-2023-11-24-192550.jpg]
Reply


Messages In This Thread
A Question on Calls to Subroutines - by Dimster - 11-22-2023, 04:43 PM
RE: A Question on Calls to Subroutines - by Kernelpanic - 11-24-2023, 07:02 PM



Users browsing this thread: 3 Guest(s)