Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

function() inputs and outputs

Question posted by: TheServant (Needs Regular Fix) on May 16th, 2008 01:02 PM
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!

Expand|Select|Wrap|Line Numbers
  1. function untrainer ( $un, $rec, $reg, $exp, $vet, &$new_rec, &$new_reg, &$new_exp, &$new_vet ) {
  2.     $new_reg = $reg; $new_exp = $exp; $new_vet = $vet;
  3.     if ( $rec >= $un ) {
  4.         $new_rec = $rec - $un ;
  5.     } else {
  6.         $new_rec = $un - $rec;
  7.         $un = $un - $rec;
  8.         if ($reg >= $un ) {
  9.             $new_reg = $reg - $un ;
  10.         } else {
  11.             $new_reg = $un - $reg;
  12.             $un = $un - $reg;
  13.             if ($exp >= $un ) {
  14.                 $new_exp = $exp - $un ;
  15.             } else {
  16.                 $new_exp = $un - $exp;
  17.                 $un = $un - $exp;
  18.                 $new_vet = $un - $vet;
  19.             }
  20.         }
  21.     }
  22. }
  23.  
  24. untrainer ( $un, $rec_soldier, $reg_soldier, $exp_soldier, $vet_soldier, &$new_soldier_rec, &$new_soldier_reg, &$new_soldier_exp, &$new_soldier_vet );
ronverdonk's Avatar
ronverdonk
Moderator
4,139 Posts
May 16th, 2008
11:18 PM
#2

Re: function() inputs and outputs
I have no idea of the problem, but maybe that is because I am not a gamer.

Ronald

Reply
TheServant's Avatar
TheServant
Needs Regular Fix
462 Posts
May 17th, 2008
03:18 AM
#3

Re: function() inputs and outputs
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!

Reply
Atli's Avatar
Atli
Moderator
2,523 Posts
May 17th, 2008
07:39 AM
#4

Re: function() inputs and outputs
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:
Expand|Select|Wrap|Line Numbers
  1. function increment(&$value) {
  2.   $value++;
  3. }
  4.  
  5. $number = 1;
  6. increment($number);
  7. echo $number; // Should be 2

Reply
dlite922's Avatar
dlite922
Site Addict
561 Posts
May 17th, 2008
10:00 AM
#5

Re: function() inputs and outputs
Quote:
I have no idea of the problem, but maybe that is because I am not a gamer.

Ronald


*clueless too*


Message too short.

Reply
TheServant's Avatar
TheServant
Needs Regular Fix
462 Posts
May 17th, 2008
02:23 PM
#6

Re: function() inputs and outputs
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:
Expand|Select|Wrap|Line Numbers
  1. function dothis(&$a, &$b, &$c, &$d) {
  2. $a++;
  3. $b++;
  4. $c++;
  5. $d++;
  6. }
  7. dothis($a, $b, $c, $d);
  8. echo($a.$b.$c.$d);


Also do the names used in the function need to be the same as the calling? Or can it be like:
Expand|Select|Wrap|Line Numbers
  1. function dothis(&$a, &$b, &$c, &$d) {
  2. $a++;
  3. $b++;
  4. $c++;
  5. $d++;
  6. }
  7. dothis($f, $g, $h, $i);
  8. echo($f.$g.$h.$i);

Reply
TheServant's Avatar
TheServant
Needs Regular Fix
462 Posts
May 17th, 2008
03:51 PM
#7

Re: function() inputs and outputs
Problem solved. My post above was correct, so thanks to everyone for replying, but a special thanks for Atli whose solution I used!

Reply
ronverdonk's Avatar
ronverdonk
Moderator
4,139 Posts
May 17th, 2008
05:36 PM
#8

Re: function() inputs and outputs
Quote:
Also do the names used in the function need to be the same as the calling? Or can it be like:
Expand|Select|Wrap|Line Numbers
  1. function dothis(&$a, &$b, &$c, &$d) {
  2. $a++;
  3. $b++;
  4. $c++;
  5. $d++;
  6. }
  7. dothis($f, $g, $h, $i);
  8. echo($f.$g.$h.$i);
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

Reply
Reply
Not the answer you were looking for? Post your question . . .
189,872 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top PHP Forum Contributors