functions affecting other functions?

Recommended Posts

Hello, my question is, is there a way to make *int's or other variables affect variables in another function? for instance, if i were to have *int = health;* in my main() function, would there be a way to affect/implement the same *int = health;* in another function that I had created? The reason i ask, is because i'm making a text-based game, and i'm running into some problems managing my characters' health without being to affect the *health* variable in both my main() and battle() functions. (the battle() function is, obviously, the function that manages the battles that take place in the game). thanks in advance~~ -Lucas

Share on other sites
#include <iostream>struct Character{	int health;};void hurt( Character& c ){	c.health -= 5;	std::cout << "hurting character, new health = " << c.health << '\n';}int main(){	Character ch;	ch.health = 20;	hurt( ch );	hurt( ch );	std::cout << "character's health is " << ch.health << '\n';}

Share on other sites
Yes, you surely can.

Two ways you can do it.

1) The global way.

For this you declare the variable globally. Something like this:
int health = 100;void decreaseHealth(){    health--;}int main(){    cout<<health;    return 0;}

Guess what? Yup, cout<<health is equilivant to cout<<"99"; All you do is put the variable somewhere out of the function.

2) The parameter way.

This way is the better and the most prefered. You make a function that accepts a variable into it:
void addSeven(int &theNum){    theNum += 7; // the number = it plus 7}int main(){    int num = 0;    addSeven(num);    return num;}

All you do is supply the variable in the paranthesis, as if you declare it in the parenthasis. I used the '&' sign because I want it directly, otherwise I would recieve a copy and modifying the copy will not modify the variable itself.

Good luck!

Share on other sites
thanks for your replies...!

i think for my purpose (i need it so that in the story, if the player loses health during the battle() sequence, he retains that amount of health throughout the story until he gets to another battle() sequence, in which he will start out with the same amount of health that he had at the end of the last battle() sequence).

im not sure how to use parameters, would you explain it to me? If you don't have enough time, i can just google it =]

thanks again!

Share on other sites
Quote:
 I used the '&' sign because I want it directly, otherwise I would recieve a copy and modifying the copy will not modify the variable itself.

Technical note: This is called "passing by reference" and what gets passed in is a pointer (well, reference actually...) to where the variable stored. The alternative passes in whatever value was stored in that memory location, but does not tell the function where the variable itself is, and so it can manipulate (and, of course, return) the value itself, but not put the new value into the original location of the variable (and so the original variable is left behind, and the "new" one is erased at the end of the function when it goes out of scope).

Share on other sites
Quote:
 Original post by lucasloredoim not sure how to use parameters, would you explain it to me? If you don't have enough time, i can just google it =]

Basically:

You need to know what type (int, char, etc.) of variable you will be passing into the function. Then you need to do something like

void myFunction(/*[here goes the type you found above]*/ myParameter){   /*[stuff here]*/}

Then, when you want to use ("call") the function, you fill the parentheses with whatever you want to pass into the function like

int foo = 7;myFunction(foo); /*["foo" becomes "myParameter" in the function definition above]*/

Note that if, in the above code, I tried to again access "foo," it would still be seven (even if myParameter was changed in myFunction). To get around this, you can either pass by reference (with the '&' as above), or just return the variable. To do this, you need to give my function a return type (note that "void" becomes "int") and then return myParameter, like

int myFunction(/*[here goes the type you found above]*/ myParameter){   /*[stuff here]*/   return myParemeter;}

Keep in mind, this still won't change "foo" in the example above, but if you do

int foo = 7;foo = myFunction(foo); /*[note the "foo ="]*/

you can assign whatever myFunction returns to the variable foo. You can also do someOtherVariable = myFunction(foo) to assign the result of myFunction to a different variable.

Share on other sites
What you really should do is sit down with a nice book and learn C++ properly. I highly highly suggest this book: http://www.amazon.com/exec/obidos/ASIN/020170353X

Trying to learn in this environment (a message board with 20 approaches and ideas flying at you) isn't going to be very effective in making you a productive programmer.

Share on other sites
Quote:
Original post by cowsarenotevil
Quote:
 I used the '&' sign because I want it directly, otherwise I would recieve a copy and modifying the copy will not modify the variable itself.

Technical note: This is called "passing by reference" and what gets passed in is a pointer (well, reference actually...) to where the variable stored. The alternative passes in whatever value was stored in that memory location, but does not tell the function where the variable itself is, and so it can manipulate (and, of course, return) the value itself, but not put the new value into the original location of the variable (and so the original variable is left behind, and the "new" one is erased at the end of the function when it goes out of scope).

... I know that, I was trying not to be too confusing. ...

Share on other sites
thanks for your replies:

@rdragon, thanks for your suggestion, however i already have the book "Beginning C++ Game Programming" by Michael Dawson. I am learning and progressing through that, however I was just inquiring to see if anyone had any quick solutions for my problem.

thanks again everyone for your replies

-Lucas

Share on other sites
Quote:
 Original post by lucasloredothanks for your replies:@rdragon, thanks for your suggestion, however i already have the book "Beginning C++ Game Programming" by Michael Dawson. I am learning and progressing through that, however I was just inquiring to see if anyone had any quick solutions for my problem.thanks again everyone for your replies-Lucas

Beginning C++ Game Programming is a great book. I learned from it. Read more into it and it explains parameters and that stuff + references, too.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• Forum Statistics

• Total Topics
627762
• Total Posts
2978967

• 11
• 10
• 10
• 23
• 14