SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    passing array variables to functions

    I am trying to pass an array to a function, load and manipulate the values in the array and have the caller use the new values in the array.
    Code:
           var time_elements = new Array();
           time_elements = [02,09,08,44];  // set dummy values so I can monitor
           TimeParse(document.form.hidden_input.value, time_elements);       //a time value from database
    
       function TimeParse(time_str, time_array)
       {
           time_array = time_str.split(":");
           // some more assignments to time_array[0] &[3] etc.
       }
    I assume that time_elements is passed by reference so the function can update the true values. I usually use a different name of my passed variables in functions since I will reuse them in other code and try to catch myself from using them out of scope.

    I am very familiar with writing functions from my days coding C. So I may be using some improper techniques in Javascript.

    I have checked out the code in the Venkman debugger (a treat and a challenge since this is a page from a PHP script). The original values are always in the array after I return from the function.

    I must be missing something on passing variables to functions. I have been checking the O'Reilly book Javascript The Definitive Guide (3rd ed.) by Flanagan but I haven't found any help there.

    Can anyone enlighten me on using arrays in functions?

  2. #2
    SitePoint Member
    Join Date
    Sep 2002
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have finally asnwered my own question.

    I appears that Javascript creates copies of variables passed to functions. When I return the updated array everything works as designed.
    Code:
           time_elements = TimeParse(document.form.hidden_input.value, time_elements);       //a time value from database
    
    
       function TimeParse(time_str, time_array)
       {
           time_array = time_str.split(":");
           // some more assignments to time_array[0] &[3] etc.
           return (time_array);
       }
    I thought I tried this yesterday but couldn't get it to work. I did clean a couple of other errors so today the code works properly.

    Maybe there is a better book out there that would explain this.

  3. #3
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ya...receiving a variable into a function is different than creating a reference or pointer, as is what normally happens when 'duplicating' arrays. So, your solution is proper...it should have to return the value. Functions are supposed to act autonomously from their environment, and this situation stays true to that.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





Bookmarks

Posting Permissions

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