passing data from one function to another in C

Reply

Join Date: Oct 2006
Posts: 730
Reputation: hbk619 is an unknown quantity at this point 
Solved Threads: 7
hbk619's Avatar
hbk619 hbk619 is offline Offline
Master Poster

passing data from one function to another in C

 
0
  #1
Mar 21st, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: passing data from one function to another in C

 
0
  #2
Mar 21st, 2007
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
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 730
Reputation: hbk619 is an unknown quantity at this point 
Solved Threads: 7
hbk619's Avatar
hbk619 hbk619 is offline Offline
Master Poster

Re: passing data from one function to another in C

 
0
  #3
Mar 22nd, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,031
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: passing data from one function to another in C

 
0
  #4
Mar 22nd, 2007
Originally Posted by hbk619 View Post
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 730
Reputation: hbk619 is an unknown quantity at this point 
Solved Threads: 7
hbk619's Avatar
hbk619 hbk619 is offline Offline
Master Poster

Re: passing data from one function to another in C

 
0
  #5
Mar 22nd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,031
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: passing data from one function to another in C

 
0
  #6
Mar 22nd, 2007
Originally Posted by hbk619 View Post

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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: passing data from one function to another in C

 
0
  #7
Mar 22nd, 2007
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 730
Reputation: hbk619 is an unknown quantity at this point 
Solved Threads: 7
hbk619's Avatar
hbk619 hbk619 is offline Offline
Master Poster

Re: passing data from one function to another in C

 
0
  #8
Mar 22nd, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: passing data from one function to another in C

 
0
  #9
Mar 22nd, 2007
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. }
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,031
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: passing data from one function to another in C

 
0
  #10
Mar 22nd, 2007
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]
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC