SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru DeNasio's Avatar
    Join Date
    May 2001
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Output of a function

    How do you make a function that is really a function and not a procedure?

    Let's say I want to make a function that calculates the product of two variables $x and $y. How can I assign the output of the function to a third variable $z? Take at the code below.

    Code:
    $z = Product($x,$y);
    
    function Product($x,$y) {
    $result = $x * $y;
    }
    P.S. This is an example! I don't want to hear "Why don't you just use $z = $x * $y?"!
    Last edited by DeNasio; Jun 15, 2001 at 04:35.

  2. #2
    SitePoint Evangelist
    Join Date
    Feb 2000
    Location
    England
    Posts
    568
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in your function add the following line at the end (before the } )

    return $result;

    that will return the value of $result as the value for $z.

  3. #3
    SitePoint Addict
    Join Date
    Jun 2001
    Location
    Newcastle, England
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use "return".

    Code:
    $z = Product($x,$y);
    
    function Product($x,$y) {
    $result = $x * $y;
    return $result;
    }
    Or, in this case, even simpler would be

    Code:
    $z = Product($x,$y);
    
    function Product($x,$y) {
    return $x * $y;
    }
    http://www.php.net/manual/en/functions.php is the page from the PHP manual explaning functions fully.

    [Edit - beaten to it again ]

  4. #4
    SitePoint Guru DeNasio's Avatar
    Join Date
    May 2001
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!


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
  •