Returning bool variable from function

So Im trying to code snake, and when I try to fix so that the food cannot spawn ontop of the snake the code works as I want it to. But the function wont return the value of the variable "bug" from function "bugCheck" and I cannot figure out why.

Any help is appreciated, cheers! :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  int snakeFuncs::food(int randX[2], int randY[2]) {
	//declerations
	bool bug = false;

	do {
		//gives variables random values 1-18
		randX[0] = rand() % 17 + 2;
		randY[0] = rand() % 17 + 2;

		bugCheck(randX, randY, bug);
	} while (bug == true);

	//Places cursor on generated variables and prints food
	gotoxy(randX[0], randY[0]);
	cout << yellow << "*";

	//return necessary variables
	return randX[2], randY[2];
}//end of function

//Checks for potential bugs in the game and fixes them
int snakeFuncs::bugCheck(int randX[2], int randY[2], bool bug) {
	//sets bool to true if food spawns on snake, loops as many times as snakesize
	for (int i = 0; i < snakeSize; i++) {
		if (randX[0] == snakeX[i] && randY[0] == snakeY[i]) {
			//sets bool to true
			bug = true;
		}//end of statement
	}//end of loop

	//returns necessary variables
	return bug;
}//end of function 
what if you change the bugCheck function type to "bool" instead of "int"?
@imimoises Ive tried it, but it doesnt help. And since bool also can be used with 1s and 0s it should work with int, no?
How do you know that the function won't return the bool value? You're not saving or using the return value from bugCheck() so what do you expect to happen?

@jlb I am using it, check line 11, the loop will start looping if the if statement is true

Ill note that I changed the code a bit and added an else:

1
2
3
4
5
6
7
		if (randX[0] == snakeX[i] && randY[0] == snakeY[i]) {
			//sets bool to true
			bug = true;
		}//end of statement
		else {
			bug = false;
		}//end of statement 
Last edited on
1
2
3
4
5
6
7
	do {
		//gives variables random values 1-18
		randX[0] = rand() % 17 + 2;
		randY[0] = rand() % 17 + 2;

		bugCheck(randX, randY, bug);
	} while (bug == true);


bugCheck has a return variable, but you aren't saving it anywere,

what if you put

1
2
3
4
5
6
7
8
	do {
		//gives variables random values 1-18
		randX[0] = rand() % 17 + 2;
		randY[0] = rand() % 17 + 2;

		bug = bugCheck(randX, randY, bug);
	} while (bug == true);
@imimoises I assumed if I returned bug with a new value it would automaticly get saved in the last function with the new value since I declared it on line 3? If thats not the case, than Im misstaken on how return works in functions. I recently started to use this, Ive only been using global variables, quite new to programing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  int snakeFuncs::food(int randX[2], int randY[2]) {
	//declerations
	bool bug = false;

	do {
		//gives variables random values 1-18
		randX[0] = rand() % 17 + 2;
		randY[0] = rand() % 17 + 2;

		bugCheck(randX, randY, bug);
	} while (bug == true);

	//Places cursor on generated variables and prints food
	gotoxy(randX[0], randY[0]);
	cout << yellow << "*";

	//return necessary variables
	return randX[2], randY[2];
}//end of function

//Checks for potential bugs in the game and fixes them
int snakeFuncs::bugCheck(int randX[2], int randY[2], bool bug) {
	//sets bool to true if food spawns on snake, loops as many times as snakesize
	for (int i = 0; i < snakeSize; i++) {
		if (randX[0] == snakeX[i] && randY[0] == snakeY[i]) {
			//sets bool to true
			bug = true;
		}//end of statement
	}//end of loop

	//returns necessary variables
	return bug;
}//end of function  


Anyway, I tried it your way, but it still didnt work
Look at this snippet:
1
2
3
4
5
6
7
8
9
	//declerations
	bool bug = false;

	do {
		//gives variables random values 1-18
		randX[0] = rand() % 17 + 2;
		randY[0] = rand() % 17 + 2;

c	} while (bug == true);


Since you never change bug to true this loop will never end. Remember the parameter bug in bugCheck() is sent by value, meaning that any changes made in that function are lost when the function returns.

bugCheck() is designed to return an int, yet you never use that return value. I recommend changing the function to the following:
bool bugCheck(int randX, int randY);
Then use the return value:
1
2
3
		bug = bugCheck(randX, randY);
	} while (bug == true);


Also there is no reason to pass bug to the function, just create a bool variable local to the check() function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//Checks for potential bugs in the game and fixes them
bool snakeFuncs::bugCheck(int randX[2], int randY[2], bool bug) {
	//sets bool to true if food spawns on snake, loops as many times as snakesize
        bool bug = false;
	for (int i = 0; i < snakeSize; i++) {
		if (randX[0] == snakeX[i] && randY[0] == snakeY[i]) {
			//sets bool to true
			bug = true;
		}//end of statement
	}//end of loop

	//returns necessary variables
	return bug;
}//end of function   


Also remember you can only return one value from a function using the return statement.

Topic archived. No new replies allowed.