I have my variables stated globally, and at the end of each function, I want them to be updated globally, so that the next function is using the right numbers.
Does return do this? How do I go about using it?
I have my variables stated globally, and at the end of each function, I want them to be updated globally, so that the next function is using the right numbers.
Does return do this? How do I go about using it?
what? I'm not 100% sure what you are asking, but return just returns a value from a function
So if I go...
return num;
it will take the variable that num and return it to where it was declared?
maybe. perhaps if you explain exactly what it is you are trying to do with a small accompanying code sample we can show you how to get the effect you are after.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
Ok, I was asking the wrong question, sorry.
If I want to return a variable, does it need to be stated with the function?
As in, should it look like this?
More importantly, if I have a variable stated globally, every time I do something to it like change it's value (such as v = 15), wouldn't it be 15 in the next function I use it in? Regardless of returning it or not?Code:#include <iostream> int main(int num); int sta(); int num; using namespace std; int sta() { cout<<"Number is "<<num<<endl; system("pause"); } int main(int num) { num = num + 1; return num; }
Last edited by Flakster; 10-13-2005 at 12:32 PM.
yes. If a variable is global, you can access it from everywhere and in a single threaded program then your last statement above would be true. Your code is flawed mind you.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
Oops, nevermind, I figured out what I was doing wrong.
I was using return properly, but I wasn't in the right spot.
Sorry for horribly vague questions.
Yeah, that was kind of pseudo/very crappy code I threw together, then I realized I was pretty much doing it right already, but I was thrown off by a simple mistake elsewhere in my code.
Thanks everyone!
Last edited by Flakster; 10-13-2005 at 12:58 PM. Reason: spelling mistake :):):):):):)
You really don't have to use the return to access the new data of num it is global so any function that modifies it will actually modify it and the next time it is used it will have those modifications done already I would recommend to you not to use globals they can get you into trouble sometimes.
Woop?