BlitzMax Pass integers by reference & function scope

Started by PixelOutlaw, March 24, 2025, 14:33:59

Previous topic - Next topic

PixelOutlaw

Hello,

I've currently got two questions.
I see GetColor seems to be able to use references to integers in order to assign them back to the parameters.
Is this a language feature that exists only in the implementation and is not available to me?

Code: BASIC
Local r:Int
Local g:Int
Local b:Int
' GetCclor assigns INTEGER (which is not a Type) parameters by reference
' How can I write functions like this?
GetColor r, g, b

Print "R " + r + " G " + g + " B " + b

Also, it seems that BlitzMax nested functions have no notion of their parents' local variables.
It would be very nice to be able to modify several parent's local values at once.
Code: BASIC
Function main()
    Local value:Int = 0
    Local value2:Int = 0
    
    ' Why can't this "see" the variable value?
    ' It would seem BlitzMax functions have no notion of parent scope
    Function foo()
        value = 42
        value2 = 43
    End Function

    foo()
    Print value
    Print value2
End Function

In other languages which support internal functions, this seems to work fine.

Python example:
Code: BASIC
def main():
    value = 0
    
    def foo():
        nonlocal value
        value = 42
        
    foo()
    print(value)

Or Scheme:
Code: BASIC
(define (main)
  (let* ((value 0)
         (foo (lambda () (set! value 42))))
    (foo)
    (display value)))


Will print 42
Both have access to the values at the point at which the function is defined.
Ubuntu MATE 20.04: i5-3570K CPU @ 3.40GHz, 8GB RAM, GeForce GTX 1060 3GB

One DEFUN to rule them all, One DEFUN to find them, One DEFUN to RETURN them all, and in the darkness MULTIPLE-VALUE-BIND them.

Henri

Hi,

you can use var keyword after the parameter to pass value by reference.

Code: BASIC
SuperStrict

' the following illustrates parsing function parameters by reference

Function ReturnMultiplevalues(a:Int Var,b:Int Var,c:Int Var)
 a=10
 b=20
 c=30
 Return
End Function

Local x:Int,y:Int,z:Int

ReturnMultipleValues(x,y,z)

Print "x="+x '10
Print "y="+y '20
Print "z="+z '30

-Henri
- Got 01100011 problems, but the bit ain't 00000001

PixelOutlaw

Quote from: Henri on March 24, 2025, 15:21:32Hi,

you can use var keyword after the parameter to pass value by reference.

Code: BASIC
SuperStrict

' the following illustrates parsing function parameters by reference

Function ReturnMultiplevalues(a:Int Var,b:Int Var,c:Int Var)
 a=10
 b=20
 c=30
 Return
End Function

Local x:Int,y:Int,z:Int

ReturnMultipleValues(x,y,z)

Print "x="+x '10
Print "y="+y '20
Print "z="+z '30

-Henri

Excellent! Thank you very much.
Ubuntu MATE 20.04: i5-3570K CPU @ 3.40GHz, 8GB RAM, GeForce GTX 1060 3GB

One DEFUN to rule them all, One DEFUN to find them, One DEFUN to RETURN them all, and in the darkness MULTIPLE-VALUE-BIND them.