| | |
passing data from one function to another in C
![]() |
I got this code (written when sleepy, sorry for lack of "expertness")
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
C Syntax (Toggle Plain Text)
#include <stdio.h> #include<time.h> int card1; int card2; int card3; int total; int other; srand(time(NULL)); card1=(rand()%22)+1; card2=(rand()%22)+1; other=(rand()%22)+1; total=card1+card2; int main() { printf("Hey, welcome to HBK's Twist or bust game. Lets begin\n"); printf("Your cards are: %d and %d\n", card1, card2); printf("total is: %d\n", total); if(total<21) { turn(); } else if (total>21); { printf("BUST! play again?\n"); } return 0; }
C Syntax (Toggle Plain Text)
#include<stdio.h> void turn(); void turn() { char ans; printf("Twist or bust (t/b)\n"); ans = getchar(); switch(ans) { case 't' : draw(); break; case 'b' : neg(); break; } }
C Syntax (Toggle Plain Text)
#include <stdio.h> void neg(); void neg() { if(total > other) { printf("you won! play again?\n"); } else if (other > total) {printf("damn nice try. play again?\n"); } }
C Syntax (Toggle Plain Text)
#include<stdio.h> void draw(); void draw() { srand(time(NULL)); card3=(rand()%22)+1; total+=card3; }
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
Use parameters to pass data between functions.
For example:
A few problems with your code:
For example:
c Syntax (Toggle Plain Text)
void myFunction(int amount); // etc. myFunction(aNumberUserEntered); } void myFunction(int amount) { // 'amount' contains whatever was passed inside the () // blah blah }
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
getcharbecause you aren't removing the newline left behind
"Technological progress is like an axe in the hands of a pathological criminal."
thanks, i moved the variables, and put everything into one files so it looks like this:
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?
C Syntax (Toggle Plain Text)
#include <stdio.h> #include<time.h> int main() { int card1; int card2; int card3; int total; int other; srand(time(NULL)); card1=(rand()%22)+1; card2=(rand()%22)+1; other=(rand()%22)+1; total=card1+card2; printf("Hey, welcome to HBK's Twist or bust game. Lets begin\n"); printf("Your cards are: %d and %d\n", card1, card2); printf("total is: %d\n", total); if(total<21) { turn(total, other); } else if (total>21); { printf("BUST! play again?\n"); } return 0; } void turn(int total, int other); void turn(int total, int other) { char ans; printf("Twist or bust (t/b)\n"); ans = getchar(); while (getchar() != '\n'); switch(ans) { case 't' : draw(total); break; case 'b' : neg(total, other); break; } } void draw(int total); void draw(int total) { srand(time(NULL)); int card3=(rand()%22)+1; total+=card3; total=(int) total; /*will do switch for play again*/ } void neg(int total, int other); void neg(int total, int other) { if(total > other) { printf("you won! play again?\n"); } else if (other > total) {printf("damn nice try. play again?\n"); } /*going to do a switch block here linking back to main()*/ }
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?
•
•
•
•
C Syntax (Toggle Plain Text)
void turn(int total, int other); void draw(int total); void neg(int total, int other);
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.
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?
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

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?
C Syntax (Toggle Plain Text)
#include <stdio.h> #include<time.h> void turn(int total, int other); void draw(int total, int other); void neg(int total, int other); void eval(int total, int other); int main() { int card1; int card2; int card3; int total; int other; srand(time(NULL)); card1=(rand()%13)+1; card2=(rand()%13)+1; other=(rand()%13)+1; total=card1+card2; printf("Hey, welcome to HBK's Twist or bust game. Lets begin\n"); printf("Your cards are: %d and %d\n", card1, card2); printf("total is: %d\n", total); eval(total, other); return 0; } void eval(int total, int other) { char again; if(total<21) { turn(total, other); } if (total>21); { printf("BUST! play again?\n"); } again = getchar(); while (getchar() != '\n'); switch(again) { case 'y' : main(); break; case 'n' : printf("ok, see ya!\n"); break; } } void turn(int total, int other) { char ans; printf("Twist or bust (t/b)\n"); ans = getchar(); while (getchar() != '\n'); switch(ans) { case 't' : draw(total, other); break; case 'b' : neg(total, other); break; } } void draw(int total, int other) { srand(time(NULL)); int card3=(rand()%13)+1; printf("next card is: %d\n", card3); total+=card3; printf("new total: %d\n", total); total=(int) total; eval(total, other); } void neg(int total, int other) { char win, lose; if(total > other) { printf("Opponents cards: %d\n", other); printf("you won! play again?\n"); win = getchar(); while (getchar() != '\n'); switch(win) { case 'y' : main(); break; case 'n' : printf("ok, see ya!\n"); break; }} else if (other > total) {printf("Opponents cards: %d\n", other); printf("damn nice try. play again?\n"); lose = getchar(); while (getchar() != '\n'); switch(lose) { case 'y' : main(); break; case 'n' : printf("ok, see ya!\n"); break; }} }
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.
•
•
•
•
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?
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.
C Syntax (Toggle Plain Text)
#include <stdio.h> /* function not returning a value */ void double_not(int num) { num *= num; /* integer is being doubled, but nothing is returned */ } /* same function returning a value */ int double_it(int num) /* a integer is being passed */ { return num *= num; /* return that integer doubled */ } int main(void) { int number = 2; printf("What's in number?: %d\n", number); double_not(number); printf("What's in number after double_not function?: %d\n", number); number = double_it(number); /* number is being here modified */ printf("What's in number after double_it function?: %d\n", number); getchar(); return 0; }
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.
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.
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.
c Syntax (Toggle Plain Text)
void doubleValue (int i) { i = i * 2; printf ("\ni inside function is %d", i); } int main (void) { int i = 9; printf ("\ni before function call is %d", i); doubleValue (i); printf ("\ni after function call is %d",i); getchar (); return 0; }
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.
ok, that makes sense. so i need to use the return keyword to change the original value (up date it)
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?
C Syntax (Toggle Plain Text)
int draw(int total, int other) { srand(time(NULL)); int card3=(rand()%13)+1; printf("next card is: %d\n", card3); total+=card3; printf("new total: %d\n", total); total=(int) total; return total; eval(total, other); }
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
Don't know what this line's supposed to do:
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.
c Syntax (Toggle Plain Text)
total=(int) total;
>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.
c Syntax (Toggle Plain Text)
// ... total = draw(blah, blah); }
"Technological progress is like an axe in the hands of a pathological criminal."
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]
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]
![]() |
Similar Threads
- Passing data from forms from one page to another (JavaScript / DHTML / AJAX)
- Please help with passing data from one form to another form (VB.NET)
- Passing data from datagridview to textboxes (VB.NET)
- Passing a matrix from main function to user defined function. (C++)
- need halp passing array data (C#)
- AnsiString Template Data Return Problem Builder 6 (C++)
- Data Abstraction (Computer Science)
Other Threads in the C Forum
- Previous Thread: c and argv[]
- Next Thread: Linked List
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






