A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: How do functions work?

  1. #1
    Member
    Join Date
    Apr 2005
    Posts
    72

    How do functions work?

    I wonder how do functions work with parameters by REFERENCE.

    Let's say in C++ you will write
    function (&nameOfVar) the name of the var takes a "&" if you want to pass it by reference, but what is it in Flash?

    Because I want that the vars I use in the functions to get changed in the end...

  2. #2
    OOP is one letter from OOPS kortex's Avatar
    Join Date
    Aug 2005
    Location
    New Hope, PA
    Posts
    2,668
    If I understand you correctly, you want to pass a variable to a function, modify the value, and then return it?

    If so:
    //define the function
    function changeValue(x){
    x = someNewValue;
    return x;
    }

    then when you call the function as such:
    //with a number
    var changedX = changeValue(5)
    //with a string
    var changedX = changeValue("bob")

    will set the new variable var changedX to whatever the function does to the variable because you told the function to return a value at the end.
    Jeremy Wischusen
    Flash - Flex - LAMP - Web Developer Purple Inc
    AS OOP FAQ-Best Practices Thread | Flashkit OOP Tutorials | Purple Inc (day job) | Blog


  3. #3
    ___________________
    Join Date
    May 2004
    Posts
    3,174
    flash uses loose-typed variables.
    this following won't output errors as would happen in a strict-typed environment:
    Code:
    var A=10;
    if(A > 10)A=getTimer();
    function bob(A){
    if(!A) var A = Math.max(this.A,_global.A)||arguments.callee.A;}

  4. #4
    Member
    Join Date
    Apr 2005
    Posts
    72
    I'm sorry, perhaps I wasn't clear enough... I mean passing by reference for functions that could have more than 1 vars.

    e.g:

    func = function(a, b, c) {
    a=10
    b=a+5
    c=0
    }

    h = i = j = 0

    func(h, i, j)


    let's say you would like to change those 3 values, you can't use return with 3 variables...


    In C++, that would have been: function(&a, &b, &c) ... To tell that those passed values will get changed...

    Someone have a clue?

  5. #5
    OOP is one letter from OOPS kortex's Avatar
    Join Date
    Aug 2005
    Location
    New Hope, PA
    Posts
    2,668
    Well on way you coulod do this if these variables have to be updated regularly is to define the globally or on the root. If they are on the root, they can be accessed both within and outside of the funcntion. Hence when the fuinction updates them they will eb avaiable anywhere else in the movie you would like to use them. So:


    try this so you can see what I mean (i will build on your example). Creat a blank movie and put this code on the first frame of a layer then run it and take a look at the output panel:
    var a:Number = 0
    var b:Number = 0
    var c:Number = 0
    trace("a = " + a + " b = " + b + " c = " + c)

    function changeValues(a, b, c) {
    a=10
    b=a+5
    c=0
    }
    trace("a = " + a + " b = " + b + " c = " + c)

    perhaps some other have a more efficient way of doing this.
    Jeremy Wischusen
    Flash - Flex - LAMP - Web Developer Purple Inc
    AS OOP FAQ-Best Practices Thread | Flashkit OOP Tutorials | Purple Inc (day job) | Blog


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