first off i apologize for my ignorance but i cant seem to find a clear answer to this question, if i have a function with variables in it, do those variables reset every time i call the function for example if i write code to build a game and then have a function such as this to count score:

void scorecounter(bool playgame, bool playagain){
int win;
int loss;
if (playgame==false){loss++;}
else if(playgame==true){win++;}
if(playagain==false){cout<<"Wins: "<<win<<" Losses: "<<loss;}
}

note that playGame and playagain work in seperatly declared functions.

the thing is i have to call this function after every time the user plays a game and then use this to record if they have won or lost, the thing is does win and loss reset after evertime i call this function therefore making it useless? do i need global varaiables? thanks!

Firstly all variables are scoped. That means that if a variable is within a set of brackets { } then the variable will exist only within that region.
Each time the execution leaves that region, the variable is said to be out of scope.

int myFunc()
{
   int A(10);
   if (A!=8)
     {
       int B=37;
       std::cout<<"B is equal to "<<B<<std::endl;
     }
   // This line will compile:
   std::cout<<"A=="<<A<<std::endl;
   // This line will not compile: 
   std::cout<<"B == "<<B<<std::endl;
   return;
}

Note the line accessing B at the end of the function will not compile, because B is out of scope

All information on a variable is lost when it goes out of scope. There are a couple of exceptions,with the main one being the declaration of a static variable. e.g.

int myFunction()
{ 
  static int A(10);  
  int B(10);
  std::cout<<"A == "<<A<<" B=="<<B<<std::endl;
  A++;
  B++;
  return;
}

In this code A increases each time that you call the function, but B stays as 10.

Note: In your code you DID NOT initialize the variables win and loss. Their initial value can be ANYTHING and it can be runtime dependent, e.g. you run the program once, it works, next time it doesn't. Always initialize variables with either int win(0); or int win=0; . Both of these pieces of code are equivilent.


Global variables are normally, just variables that are defined in the "global" scope, i.e. they exist until the last point in the program.

In this case you have to figure out, (a) would you like a local variable to the function that keeps state, i.e. you don't need win/loss anywhere else in the program. Or (b) do you need a global variable.

Also, you might need an effective global variable, by wrapping up the win/loss etc in a class, which persists to the end of the program state. [Maybe even reading/writing itself from a file]

No you don't need global variables you can pass by reference or pointer, but I would suggest making a variable "static".

http://msdn.microsoft.com/en-us/library/s1sb61xd(v=vs.80).aspx

Note that the "static" keyword may behave differently than you expect in classes.

ex:

int CurrentTotal(int n)
{
  static int total = 0;
  total += n;
  return total;
}

The code I posted below shows two functions, one using global variables and the other using reference to variables, that could be used for keeping track of the score.

If it is a small game I would just use the global variables since you do not have to fill out the function parameters every time you want to use it. But I would still play around with the second option because pointers and references are very useful and can clean up code lots.

#include <iostream>
using namespace std;

int win1 = 0, loss1 = 0; //globals

void scorecounter( bool playgame, bool playagain ) //uses the global variables
{
	if( playgame == false )
		loss1++;
	else if( playgame == true )
		win1++;

	if( playagain == false )
		cout << "Wins: " << win1 << " Losses: " << loss1 << endl;
}

void scorecounter( bool playgame, bool playagain, int &win, int &loss ) //uses reference to variables in main()
{
	if( playgame == false )
		loss++;
	else if( playgame == true )
		win++;

	if( playagain == false )
		cout << "Wins: " << win << " Losses: " << loss << endl;
}


int main()
{
	int win2 = 0, loss2 = 0;
	scorecounter( true, false );
	scorecounter( true, false, win2, loss2 );
	cout << win2 << " " << loss2 << endl; //this shows that the variables have been changed by the function above

	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.