944,149 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3265
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 21st, 2007
0

passing data from one function to another in C

Expand Post »
I got this code (written when sleepy, sorry for lack of "expertness")

  1. #include <stdio.h>
  2. #include<time.h>
  3. int card1; int card2; int card3;
  4. int total;
  5. int other;
  6. srand(time(NULL));
  7. card1=(rand()%22)+1;
  8. card2=(rand()%22)+1;
  9. other=(rand()%22)+1;
  10. total=card1+card2;
  11. int main()
  12. {
  13.  
  14. printf("Hey, welcome to HBK's Twist or bust game. Lets begin\n");
  15.  
  16. printf("Your cards are: %d and %d\n", card1, card2);
  17. printf("total is: %d\n", total);
  18.  
  19. if(total<21)
  20. { turn(); }
  21.  
  22. else if (total>21);
  23. { printf("BUST! play again?\n"); }
  24. return 0;
  25. }

  1. #include<stdio.h>
  2. void turn();
  3. void turn()
  4. { char ans;
  5. printf("Twist or bust (t/b)\n");
  6. ans = getchar();
  7. switch(ans)
  8. { case 't' : draw(); break;
  9. case 'b' : neg(); break;
  10. }
  11. }


  1. #include <stdio.h>
  2. void neg();
  3. void neg()
  4. {
  5. if(total > other)
  6. { printf("you won! play again?\n"); }
  7. else if (other > total)
  8. {printf("damn nice try. play again?\n"); }
  9. }

  1. #include<stdio.h>
  2. void draw();
  3. void draw()
  4. {
  5. srand(time(NULL));
  6. card3=(rand()%22)+1;
  7. total+=card3;
  8. }

it's a card game (twist or bust). so two random numbers are drawn, if they're over 21, you lose, under and you can take another card. if the three cards are over 21 you lose, if not anohter card.. so the total is always changing, how can i pass the value of "total" between the functions? i know there's a way in java placing the variable name in the () of the method, (well i think. it didn't work but i'm new at all of this). any pointers would be appriciated, but like i said, sleepy when i wrote it
Similar Threads
Reputation Points: 273
Solved Threads: 8
Master Poster
hbk619 is offline Offline
733 posts
since Oct 2006
Mar 21st, 2007
0

Re: passing data from one function to another in C

Use parameters to pass data between functions.

For example:
  1. void myFunction(int amount);
  2.  
  3. // etc.
  4.  
  5. myFunction(aNumberUserEntered);
  6. }
  7.  
  8. void myFunction(int amount) {
  9.  
  10. // 'amount' contains whatever was passed inside the ()
  11. // blah blah
  12. }

A few problems with your code:
  • When splitting up your source files, create a .h that contains ONLY prototypes -- no actual function definitions, those go in the .cpp files
  • Try to avoid the use of globals in your program, like you did before main(). Whenever possible, pass needed data between functions. It's much easier to track down errors
  • You're going to have potential problems with getchar because you aren't removing the newline left behind
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 22nd, 2007
0

Re: passing data from one function to another in C

thanks, i moved the variables, and put everything into one files so it looks like this:

  1. #include <stdio.h>
  2. #include<time.h>
  3.  
  4.  
  5. int main()
  6. {
  7.  
  8. int card1; int card2; int card3;
  9. int total;
  10. int other;
  11.  
  12. srand(time(NULL));
  13. card1=(rand()%22)+1;
  14. card2=(rand()%22)+1;
  15. other=(rand()%22)+1;
  16.  
  17. total=card1+card2;
  18.  
  19.  
  20. printf("Hey, welcome to HBK's Twist or bust game. Lets begin\n");
  21.  
  22.  
  23. printf("Your cards are: %d and %d\n", card1, card2);
  24.  
  25. printf("total is: %d\n", total);
  26.  
  27. if(total<21)
  28. { turn(total, other); }
  29.  
  30. else if (total>21);
  31. { printf("BUST! play again?\n"); }
  32.  
  33. return 0;
  34. }
  35.  
  36. void turn(int total, int other);
  37.  
  38. void turn(int total, int other)
  39. { char ans;
  40. printf("Twist or bust (t/b)\n");
  41. ans = getchar();
  42. while (getchar() != '\n');
  43.  
  44.  
  45.  
  46. switch(ans)
  47. { case 't' : draw(total); break;
  48. case 'b' : neg(total, other); break;
  49. }
  50.  
  51. }
  52.  
  53.  
  54.  
  55. void draw(int total);
  56.  
  57. void draw(int total)
  58. {
  59. srand(time(NULL));
  60. int card3=(rand()%22)+1;
  61.  
  62. total+=card3;
  63. total=(int) total;
  64.  
  65. /*will do switch for play again*/
  66.  
  67. }
  68.  
  69.  
  70.  
  71. void neg(int total, int other);
  72.  
  73. void neg(int total, int other)
  74. {
  75. if(total > other)
  76. { printf("you won! play again?\n"); }
  77.  
  78. else if (other > total)
  79. {printf("damn nice try. play again?\n"); }
  80. /*going to do a switch block here linking back to main()*/
  81. }

when i compile that i get these errors:

main2.c:36: error: conflicting types for 'turn'
main2.c:28: error: previous implicit declaration of 'turn' was here
main2.c:55: error:conflicting types for 'draw'
main2.c:47: error: previous implicit declaration of 'draw' was here
main2.c:71: error: conflicting types for 'neg'
main2.c:48: error: previous implicit declaration of 'neg' was here


what does that mean?
Reputation Points: 273
Solved Threads: 8
Master Poster
hbk619 is offline Offline
733 posts
since Oct 2006
Mar 22nd, 2007
0

Re: passing data from one function to another in C

Click to Expand / Collapse  Quote originally posted by hbk619 ...
  1.  
  2. void turn(int total, int other);
  3. void draw(int total);
  4. void neg(int total, int other);
These prototypes should be before the main() function or in another head of file included, so the compiler can see what are they.
Also the int total and int other that you wrote inside each function are
different variables with the same name. When you pass those as a
parameter to a function you are creating a copy with the same value, if that value is the one being passed.

Just because it has the same name doesn't mean are the same variables.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Mar 22nd, 2007
0

Re: passing data from one function to another in C

I moved those three lines an it worked! except the first two numbers totaled 23 so it went straight out.. guess i should get to work on the switch statements eh? thanks for the help

Aia, a copy is being passed, that makes sense. so the total in main is total(copy1), that gets sent to turn but as total( copy2). So if i sent it back to the main (for the if>21 evaluation) would it be total(copy1) or total(copy2)thats evaluated?

  1. #include <stdio.h>
  2. #include<time.h>
  3.  
  4. void turn(int total, int other);
  5. void draw(int total, int other);
  6. void neg(int total, int other);
  7. void eval(int total, int other);
  8. int main()
  9. {
  10. int card1; int card2; int card3;
  11. int total;
  12. int other;
  13. srand(time(NULL));
  14. card1=(rand()%13)+1;
  15. card2=(rand()%13)+1;
  16. other=(rand()%13)+1;
  17. total=card1+card2;
  18.  
  19. printf("Hey, welcome to HBK's Twist or bust game. Lets begin\n");
  20.  
  21. printf("Your cards are: %d and %d\n", card1, card2);
  22. printf("total is: %d\n", total);
  23.  
  24. eval(total, other);
  25.  
  26. return 0;
  27. }
  28.  
  29. void eval(int total, int other)
  30. { char again;
  31. if(total<21)
  32. { turn(total, other); }
  33.  
  34. if (total>21);
  35. { printf("BUST! play again?\n"); }
  36. again = getchar();
  37. while (getchar() != '\n');
  38. switch(again)
  39. { case 'y' : main(); break;
  40. case 'n' : printf("ok, see ya!\n"); break;
  41. }
  42. }
  43.  
  44. void turn(int total, int other)
  45. { char ans;
  46. printf("Twist or bust (t/b)\n");
  47. ans = getchar();
  48. while (getchar() != '\n');
  49.  
  50. switch(ans)
  51. { case 't' : draw(total, other); break;
  52. case 'b' : neg(total, other); break;
  53. }
  54. }
  55. void draw(int total, int other)
  56. {
  57. srand(time(NULL));
  58. int card3=(rand()%13)+1;
  59. printf("next card is: %d\n", card3);
  60. total+=card3; printf("new total: %d\n", total);
  61. total=(int) total; eval(total, other);
  62. }
  63.  
  64.  
  65. void neg(int total, int other)
  66. { char win, lose;
  67.  
  68. if(total > other)
  69. { printf("Opponents cards: %d\n", other);
  70. printf("you won! play again?\n");
  71. win = getchar();
  72. while (getchar() != '\n');
  73.  
  74. switch(win)
  75. { case 'y' : main(); break;
  76. case 'n' : printf("ok, see ya!\n"); break;
  77. }}
  78.  
  79. else if (other > total)
  80. {printf("Opponents cards: %d\n", other);
  81. printf("damn nice try. play again?\n");
  82.  
  83. lose = getchar();
  84. while (getchar() != '\n');
  85.  
  86. switch(lose)
  87. { case 'y' : main(); break;
  88. case 'n' : printf("ok, see ya!\n"); break;
  89. }}
  90. }

thats the new one. here's a game:

Hey, welcome to HBK's Twist or bust game. Lets begin
Your cards are: 3 and 5
total: 8
Twist or bust? (t\b):
t
next card is: 10
new total: 18
BUST! play again?
n
ok, see ya!
BUST! play again?
n
ok, see ya!
BUST! play again?


It got stuck in a loop :-\ . if i don't twist and say no. then it exits as it should. why does it jump back up to the eval function?

now it's getting interesting :-D
Last edited by hbk619; Mar 22nd, 2007 at 1:55 pm.
Reputation Points: 273
Solved Threads: 8
Master Poster
hbk619 is offline Offline
733 posts
since Oct 2006
Mar 22nd, 2007
0

Re: passing data from one function to another in C

Click to Expand / Collapse  Quote originally posted by hbk619 ...

Aia, a copy is being passed, that makes sense. so the total in main is total(copy1), that gets sent to turn but as total( copy2). So if i sent it back to the main (for the if>21 evaluation) would it be total(copy1) or total(copy2)thats evaluated?
No my friend. The total being evaluated in main is not being changed
at all by the functions because you are not returning any value from
the function. So actually, nothing it is send back to main.

I am bad explaining these in words, so I'll give you this little example
and hopefully it would help to understand it.

  1. #include <stdio.h>
  2.  
  3. /* function not returning a value */
  4. void double_not(int num)
  5. {
  6. num *= num; /* integer is being doubled, but nothing is returned */
  7. }
  8.  
  9. /* same function returning a value */
  10. int double_it(int num) /* a integer is being passed */
  11. {
  12. return num *= num; /* return that integer doubled */
  13. }
  14.  
  15. int main(void)
  16. {
  17. int number = 2;
  18.  
  19. printf("What's in number?: %d\n", number);
  20.  
  21. double_not(number);
  22.  
  23. printf("What's in number after double_not function?: %d\n", number);
  24.  
  25. number = double_it(number); /* number is being here modified */
  26. printf("What's in number after double_it function?: %d\n", number);
  27.  
  28. getchar();
  29. return 0;
  30. }

To avoid confution, I don't like to give the same variable names passed as parameters to functions
Last edited by Aia; Mar 22nd, 2007 at 3:32 pm.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Mar 22nd, 2007
0

Re: passing data from one function to another in C

Simply said, in pass by value, a copy of the value of the variable is paseed to the function while the original variable remains unchanged.

  1. void doubleValue (int i)
  2. {
  3. i = i * 2;
  4. printf ("\ni inside function is %d", i);
  5. }
  6.  
  7. int main (void)
  8. {
  9. int i = 9;
  10. printf ("\ni before function call is %d", i);
  11. doubleValue (i);
  12. printf ("\ni after function call is %d",i);
  13.  
  14. getchar ();
  15. return 0;
  16. }

Run the above code and you would understand the function has its own copy of the variable "i". I used the same variables so that you would know they are not the same.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Mar 22nd, 2007
0

Re: passing data from one function to another in C

ok, that makes sense. so i need to use the return keyword to change the original value (up date it)

  1. int draw(int total, int other)
  2. {
  3. srand(time(NULL));
  4. int card3=(rand()%13)+1;
  5. printf("next card is: %d\n", card3);
  6. total+=card3; printf("new total: %d\n", total);
  7. total=(int) total; return total; eval(total, other);
  8. }

like that? will that mean the total in eval function (in my previous post) is updated? .. except apparently not because the total for one go is 15, which is less than 21 but it goes to BUST! play again?
Last edited by hbk619; Mar 22nd, 2007 at 5:00 pm. Reason: changed coding
Reputation Points: 273
Solved Threads: 8
Master Poster
hbk619 is offline Offline
733 posts
since Oct 2006
Mar 22nd, 2007
0

Re: passing data from one function to another in C

Don't know what this line's supposed to do:
  1. total=(int) total;
But whatever.

>will that mean the total in eval function (in my previous post) is updated?
Only if you assign the return value to a variable, eg.
  1. // ...
  2. total = draw(blah, blah);
  3. }
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 22nd, 2007
0

Re: passing data from one function to another in C

hbk619 your code is getting very confusing right now. And very hard
to read because is not following a proper format. Read these sugestions about formating.

Also use tags when you post your code. For example, for C you write
[code=c]at the beginning, and end of your c code[/code]
For C++ you write
[code=cplusplus]at the start of your C++ code, and end of it [/code]
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: c and argv[]
Next Thread in C Forum Timeline: Linked List





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC