SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    Under Construction Poop_Shoot's Avatar
    Join Date
    Jul 2003
    Location
    Sacramento, CA
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Functions returning more than one value?

    Is it possible to have a function return more than one value?

    return this;
    return that;
    and so on....;


    If this is possible in what order are they returned? Thanx

    Jon

  2. #2
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <script type="text/javascript">
    <!--
    function doit(isFunc)
    {
      if (isFunc)
    	 return ('a|b')
      else
    	 return ('a', 'b');
    }
    
    function doit2()
    {
      alert( doit(1).split("|"))
    }
    
    alert(doit(0))
    doit2()
    //-->
    </script>
    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  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)
    What Vincent somewhat cryptically shows, is that no, you can't return two values in the manner you suggest (a return statement exits the function).

    You can, however, return an array of values, or devise a way to parse returned information to suit your needs.
    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




  4. #4
    SitePoint Guru bronze trophy blufive's Avatar
    Join Date
    Mar 2002
    Location
    Manchester, UK
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Surely, given javascript's OO capabilities, you could also return an object? Which is just another way of returning multiple items in one hit, as vinny suggested, of course.

  5. #5
    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)
    Absolutely - can you return anything, even a function.
    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
  •