Operation on parameter doesn't affect global variable

Event functions are global variable requiring bastards. I have a variable called myNum (=55). This is sent to a function as a parameter called mN (seems myNum and mN should be the same exact thing; one is the ‘global variable’ presumably and the other is a parameter OF this same thing). But when I subtract 1 from mN every frame, it has no affect on myNum. I want operations performed on mN to affect myNum.


var myNum = 55
stage.addEventListener(Event.ENTER_FRAME, onEnter)

function onEnter(e){
    doThis(myNum)
}

function doThis(mN){
    mN -= 1
    trace(mN + ' ' + myNum)
}

//traces '54 55'... '54 55'... '54 55'... repeatedly, but clearly I am SUBTRACTING 1 each time

The problem was that primitive classes (or something like that). Are passed by value (copied) not by reference (passed).

Just don’t pass anything to the function… and operate on the myNum directly

function doThis(){
myNum -= 1
trace(mN + ’ ’ + myNum)
}

This forum is always so helpful, thanks.
That was a simplified example though. my actual function took 5 parameters, not just 1. So my guess is that a function can only return ONE variable, in which case I guessed it should be one ARRAY composed of those 5. But that doesn’t work. (this is just the same ‘return’ thing posted above but with more parameters, and an array returned OF those parameters. The array does not affect the original array like I want it to though).

Re: kookaburra- That makes sense, however the purpose of the function was to encapsulate. Like maybe I have a hundred different variables and I want to be able to call doThis with ANY of them… kinda like
variable1 = '55’
variable2 = '100’
doThis(variable1) // returns 54
doThis(variable2) // returns 99
That function is not reusable if its hard coded to always use a specific global variable.
(if any of this makes sense)


var myNum = 55
var myNum2 = 55
var myNum3 = 55
var myNum4 = 55
var myNum5 = 55
var numArray = new Array(myNum, myNum2, myNum3, myNum4, myNum5)
stage.addEventListener(Event.ENTER_FRAME, onEnter)

function onEnter(e){
    numArray = doThis(myNum, myNum2, myNum3, myNum4, myNum5)
}

function doThis(mN, mN2, mN3, mN4, mN5){
    mN -= 1
    mN2 -= 2
    mN3 -= 3
    mN4 -= 4
    mN5 -= 5
    trace(myNum + ' ' + myNum2 + ' ' + myNum3 + ' ' + myNum4 + ' ' + myNum5)
    trace(mN + ' ' + mN2 + ' ' + mN3 + ' ' + mN4 + ' ' + mN5)
    var nArray = new Array(mN, mN2, mN3, mN4, mN5)
    return nArray
}

//traces '55 55 55 55 55' / '54 53 52 51 50'.. repeatedly. Returned array doesn't return changed values.