Reputation: 105
I just started messing around with C++ but I am stuck. Any help would be much appreciated.
I want to change the value of gameState from true
to false
by passing in another function, however, my thought process seems to be incorrect:
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
void changeState(bool gameState){
string answer;
cout << "End game? Y/N" << endl;
cin >> answer;
if(answer == "Y"){
gameState = false;
} else if (answer == "N"){
gameState = true;
}
}
void gameLoop(){
bool gameState = true;
while(gameState == true){
cout << "Game is being played" << endl;
changeState(gameState);
}
}
int main(){
gameLoop();
return 0;
}
Upvotes: 0
Views: 94
Reputation: 3305
Return the gameState
varible passed by value, there is no need to pass by reference or pointer in your code, this could lead to hide behavior,
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
bool changeState(bool gameState){
string answer;
cout << "End game? Y/N" << endl;
cin >> answer;
if(answer == "Y"){
gameState = false;
} else if (answer == "N"){
gameState = true;
}
return gameState;
}
void gameLoop(){
bool gameState = true;
while(gameState == true){
cout << "Game is being played" << endl;
gameState = changeState(gameState);
}
}
int main(){
gameLoop();
return 0;
}
Upvotes: 1
Reputation: 3496
By default, function parameters are passed "by value" to c/c++ functions. This means that the function has its own local copy of the gamestate variable that is initialized to the value given by the caller.
You have 2 possibilities: to change the function to take a reference to the variable or to take a pointer to the variable instead.
A function taking a reference would look like this:
void ChangeState (bool &gameState){
gameState=false;
}
Using a pointer would look like this:
void ChangeState (bool *gameState){
*gameState=false;
}
....
ChangeState (&gameState);
Using a reference can be seen as more convenient as the syntax and the use of the local variable is not changed. On the other hand, using a pointer forces the caller to use the & to take the address of the variable, so everybody looking at the function call is made aware that the value may be changed inside the function.
Upvotes: 2
Reputation: 1566
In c++ when you pass an argument to a function it generally is passed a copy of that value, that means it is just a copy of what that variable contains, and isn't that variable. In order to create an output parameter a simple solution is to pass by reference or pointer.
I will show the reference version:
void changeState(bool& gameState){
string answer;
cout << "End game? Y/N" << endl;
cin >> answer;
if(answer == "Y"){
gameState = false;
} else if (answer == "N"){
gameState = true;
}
}
The code is the exact same the only difference is the & after the bool inside the parameter list.
This tutorial will go into more detail: http://www.cplusplus.com/doc/tutorial/functions/
Also just a fyi, a reference or pointer depending on the OS, could be 4 bytes, where your bool is typically 1 byte, just returning a bool value would take less memory then passing by reference.
Upvotes: 1