int in function vs int before function
Need some help knowing what I'm doing wrong. I was doing the palindrome question on C++ and here is the code i was using that always output that it was not a palindrome: #include <iostream> using namespace std; bool isPalindrome(int x) { //complete the function int newx, reversex, remainder; newx = x; while (newx!=0) { remainder = newx%10; reversex = reversex*10 + remainder; newx /= 10; } if (x==reversex) { return true; } else return false; } int main() { int n; cin >>n; if(isPalindrome(n)) { cout <<n<<" is a palindrome"; } else { cout << n<<" is NOT a palindrome"; } return 0; } All i had to do in order to make the code work correctly was declare int reversex before the bool function, like this: #include <iostream> using namespace std; int reversex; bool isPalindrome(int x) { //complete the function int newx, remainder; newx = x; while (newx!=0) { remainder = newx%10; reversex = reversex*10 + remainder; newx /= 10; } if (x==reversex) { return true; } else return false; } int main() { int n; cin >>n; if(isPalindrome(n)) { cout <<n<<" is a palindrome"; } else { cout << n<<" is NOT a palindrome"; } return 0; } Can anyone tell me why this is?? Thanks in advance.