Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variable as a reference or value to a function
#14
Functions will pass by reference in the exact same manner as subs.  The main rule of thumb is that variable types *must* match to pass.

Examples:
Code: (Select All)
DIM x AS INTEGER
x = 0
PRINT Foo(x)
PRINT Foo(x)
PRINT Foo(x)

FUNCTION Foo (X AS INTEGER)
  X = X + 1
  Foo = 2 * X
END FUNCTION

Now, the above will print 2, 4, 6 onto the screen, as the value of X changes and is passed back to the main routine with each run of the function.


Code: (Select All)
DIM x AS _FLOAT
x = 0
PRINT Foo(x)
PRINT Foo(x)
PRINT Foo(x)

FUNCTION Foo (X AS INTEGER)
  X = X + 1
  Foo = 2 * X
END FUNCTION

And the above will print 2, 2, 2 on the screen, as the function Foo uses an Integer, and the main module is passing a _FLOAT.  The two variable types aren't the same, so the value won't pass by reference.

For SUBs and for FUNCTIONs, variables pass by reference as long as the types match.
Reply


Messages In This Thread
RE: Variable as a reference or value to a function - by SMcNeill - 07-18-2022, 02:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Function Pointers? BlameTroi 5 265 02-20-2026, 05:55 PM
Last Post: BlameTroi
  Variable length type declarations dano 5 706 08-06-2025, 09:53 PM
Last Post: dano
  Determining if variable or static string is passed to a Sub dano 9 1,249 06-20-2025, 06:31 PM
Last Post: CookieOscar
  Most efficient way to build a big variable length string? mdijkens 9 1,920 01-17-2025, 11:36 PM
Last Post: ahenry3068
  Huge array of variable length strings mdijkens 9 1,869 10-17-2024, 02:01 PM
Last Post: mdijkens

Forum Jump:


Users browsing this thread: 1 Guest(s)