how to pass variables by reference to a function?

About Monkey 2 Forums Monkey 2 Programming Help how to pass variables by reference to a function?

This topic contains 6 replies, has 3 voices, and was last updated by  Jesse 1 year, 3 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #12848

    Jesse
    Participant

    is there a way to pass variables by reference to a function such as with BlitzMax and How?
    something like this in BlitzMax:

    Local a:int = 3
    test(a)
    Print a ‘ prints 4

    Function test(b:Int Var)
    b = 4
    End function

    #12849

    abakobo
    Participant

    Yes there is two ways:

    The safe one: use a reference based data structure i.e.: Class or Array.
    In your case an array (of size 1) is probably the best solution.

    The “unsafe way” use pointer to pass the variable by reference. You must keep the scope to the variable or you’ll get a fatal “memory access violation” runtime error.

    #12850

    Jesse
    Participant

    I knew about that and thats the way I have it on my program But I was hoping there was a simpler way like BlitzMax.
    I didn’t really want to work with pointer indexes. I feel like they are a bit messy.

    #12851

    abakobo
    Participant

    Mark has been speaking about it but I think it’s in the todo list..

    True that whether you use pointer or array you’ll end with an index!
    Class will slow down your loops.

    #12852

    Jesse
    Participant

    Ok, Thanks.

    #12855

    Mark Sibly
    Keymaster

    ‘Var’ parameters aren’t in yet but are likely to be added very soon.

    #12858

    Jesse
    Participant

    Great Mark! Thanks.

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.