d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help With C Functions
Add Reply New Topic New Poll
Member
Posts: 11,796
Joined: Feb 21 2006
Gold: 306.40
Apr 13 2014 04:13pm
How do I use variables from one function to another in C?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 13 2014 04:28pm
few options.

#1 pass the value
Code
void first(){
int magicVariable = 5;
second(magicVariable);
}
void second(int importantVariable){
// do something with importantVariable
}


#2 return value
Code
int first(){
int somethingImportant = 5;
// do stuff
return somethingImportant;
}
void second(){
int somethingImportant = first();
// do stuff
}


#3 declare it outside of the function
Code
int magicVariable = 5;
void first(){
// do something with magicVariable
}
void second(){
// do something with magicVariable
}


depends on how you wanna use it.

This post was edited by carteblanche on Apr 13 2014 04:42pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll