A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: The scope of a function

  1. #1
    Junior Member
    Join Date
    Feb 2003
    Posts
    1

    The scope of a function

    i have a function that changes a root variable that i pass into it. the problem is that the changes to the variable are only present in the function, once the function ends the new value disappears as well. here is a simple example of what i'm talking about:

    Code:
    //there's the variable i want to manipulate
    fakeVar=100;
    
    function mult(whichVar){
    //let's multiple the variable by 2.
    	whichVar*=2;
            trace(whichVar) // traces 200...correct.
            trace(fakeVar) //traces 100, the original value, not correct
    }
    
    box.onRelease=function(){
    //call the function with the variable i want to update.
    	mult(fakeVar)
    }
    the function changes the variable within its own scope, but is there a way for it to change the variable outside of its own scope as well? In my real code, the code within the function is in an onEnterFrame event, so i need it to update the variable every frame....

  2. #2
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,875
    Welcome to FK.

    When you pass in a primitive value (such as a number ) then it is passed by copy (not by reference as in the case of an object)..

    So basically in your function the parameter whichVar simply has the value 100 and is in no way connected to the variable fakeVar

    if you actually want to change the value of fakeVar then you will need to do reference it directly within the function e.g

    Code:
    var fakeVar:Number = 100;
    
    function mult(whichVar:String) {
    	this[whichVar] *= 2;
    	trace(fakeVar);
    }
    
    box.onRelease=function(){
         mult("fakeVar");
    }
    Last edited by silentweed; 08-28-2007 at 08:22 PM.
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  3. #3
    Junior Member
    Join Date
    Feb 2003
    Posts
    1

    Thanks for the advice

    wow, wouldn't have thought to structure it like that....makes perfect sense.

    while waiting for a response i came up with such a convoluted way around this problem, i was returning an array with the values i needed and then setting the variables to the corresponding array items outside the function...this will save me so much time.

    thanks!

  4. #4
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,875
    your welcome
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center