# Operation on parameter doesn't affect global variable

**URL:** <https://forum.kirupa.com/t/operation-on-parameter-doesnt-affect-global-variable/261315>\
**Category:** Uncategorized\
**Created:** [May 26, 2008, 10:14am UTC](https://forum.kirupa.com/t/operation-on-parameter-doesnt-affect-global-variable/261315 "2008-05-26T10:14:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gggmork](https://avatars.discourse-cdn.com/v4/letter/g/34f0e0/32.png) [@gggmork](https://forum.kirupa.com/u/gggmork)\
**Post date:** [May 26, 2008, 10:14am UTC](https://forum.kirupa.com/t/operation-on-parameter-doesnt-affect-global-variable/261315/1 "2008-05-26T10:14:56Z")

</div>

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.

```auto

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

```

---

<div class="post-metadata">

**Author:** ![Felixz](https://avatars.discourse-cdn.com/v4/letter/f/ecb155/32.png) [@Felixz](https://forum.kirupa.com/u/Felixz)\
**Post date:** [May 26, 2008, 11:15am UTC](https://forum.kirupa.com/t/operation-on-parameter-doesnt-affect-global-variable/261315/2 "2008-05-26T11:15:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kookaburra](https://avatars.discourse-cdn.com/v4/letter/k/f475e1/32.png) [@kookaburra](https://forum.kirupa.com/u/kookaburra)\
**Post date:** [May 26, 2008, 12:15pm UTC](https://forum.kirupa.com/t/operation-on-parameter-doesnt-affect-global-variable/261315/3 "2008-05-26T12:15:14Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![gggmork](https://avatars.discourse-cdn.com/v4/letter/g/34f0e0/32.png) [@gggmork](https://forum.kirupa.com/u/gggmork)\
**Post date:** [May 26, 2008, 12:44pm UTC](https://forum.kirupa.com/t/operation-on-parameter-doesnt-affect-global-variable/261315/4 "2008-05-26T12:44:50Z")

</div>

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)

```auto

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.

```
