function() inputs and outputs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    function() inputs and outputs

    Hey guys,
    I have an untraining script in my game which I am having a small problem with. There are 4 levels of soldiers: rec, reg, exp and vet. Now the number of soldier is recorded as the total, so when a user "untrains" some, it will take it from the total. I want to remove rec's first, reg's second, etc... Now I think the function works, but I just don't know how to do inputs and outputs. I want to automatically assign the outputs to variables, let me know if this is unclear!

    [php]function untrainer ( $un, $rec, $reg, $exp, $vet, &$new_rec, &$new_reg, &$new_exp, &$new_vet ) {
    $new_reg = $reg; $new_exp = $exp; $new_vet = $vet;
    if ( $rec >= $un ) {
    $new_rec = $rec - $un ;
    } else {
    $new_rec = $un - $rec;
    $un = $un - $rec;
    if ($reg >= $un ) {
    $new_reg = $reg - $un ;
    } else {
    $new_reg = $un - $reg;
    $un = $un - $reg;
    if ($exp >= $un ) {
    $new_exp = $exp - $un ;
    } else {
    $new_exp = $un - $exp;
    $un = $un - $exp;
    $new_vet = $un - $vet;
    }
    }
    }
    }

    untrainer ( $un, $rec_soldier, $reg_soldier, $exp_soldier, $vet_soldier, &$new_soldier_r ec, &$new_soldier_r eg, &$new_soldier_e xp, &$new_soldier_v et );
    [/php]
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    I have no idea of the problem, but maybe that is because I am not a gamer.

    Ronald

    Comment

    • TheServant
      Recognized Expert Top Contributor
      • Feb 2008
      • 1168

      #3
      I want to know how I can submit several variables to a function, and receive several variables back. Forget the game and what the function does. Just look at how I am calling the function (giving it 5 variables and receiving 4).

      Thanks for your reply tho!

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        That looks all right to me. If you change the values of the last four parameters in your function, the variables you assign to them when you call the function should change as well.

        I assume you are attempting something like this:
        [code=php]
        function increment(&$val ue) {
        $value++;
        }

        $number = 1;
        increment($numb er);
        echo $number; // Should be 2
        [/code]

        Comment

        • dlite922
          Recognized Expert Top Contributor
          • Dec 2007
          • 1586

          #5
          Originally posted by ronverdonk
          I have no idea of the problem, but maybe that is because I am not a gamer.

          Ronald
          *clueless too*


          Message too short.

          Comment

          • TheServant
            Recognized Expert Top Contributor
            • Feb 2008
            • 1168

            #6
            OK, forget the first post. I have 4 variables $a $b $c $d that I put through a function but I want the function to update them as each of their variables:
            [php]function dothis(&$a, &$b, &$c, &$d) {
            $a++;
            $b++;
            $c++;
            $d++;
            }
            dothis($a, $b, $c, $d);
            echo($a.$b.$c.$ d);[/php]

            Also do the names used in the function need to be the same as the calling? Or can it be like:
            [php]function dothis(&$a, &$b, &$c, &$d) {
            $a++;
            $b++;
            $c++;
            $d++;
            }
            dothis($f, $g, $h, $i);
            echo($f.$g.$h.$ i);[/php]

            Comment

            • TheServant
              Recognized Expert Top Contributor
              • Feb 2008
              • 1168

              #7
              Problem solved. My post above was correct, so thanks to everyone for replying, but a special thanks for Atli whose solution I used!

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                Originally posted by TheServant
                Also do the names used in the function need to be the same as the calling? Or can it be like:
                [php]function dothis(&$a, &$b, &$c, &$d) {
                $a++;
                $b++;
                $c++;
                $d++;
                }
                dothis($f, $g, $h, $i);
                echo($f.$g.$h.$ i);[/php]
                Good you solved it. As for your last question: no the names that you pass to a function are 'local' to the routine that issues the call.
                The names that you declare in the function are local to the function only, so you can name them whatever you like.

                Ronald

                Comment

                Working...