Function that returns true if even or zero and false otherwise

What is wrong with my code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
int x;
bool even(int x);
int main()
{
	int y;
	cout<<even(y);
}

bool even(int x)
{
	if ((x%2==0)||(x==0))
	return true;
	else
	return false;
	
}
Last edited on
Please post any errors you are getting otherwise it is extremely difficult to help you.

Anyway, your issue is that you must use == for comparison.

Remember that = is for assignment, == is for comparison.
What do you expect the output to be? Your y variable is uninitialized.

Try initializing the value to be 3.
1
2
int x = 3;
cout << even(y);

and therefore has a garbage value (actually undefined behavior).

Edit: Also, you have a global variable called "x" that is not doing anything and is shadowed by the x in your even function, I would not suggest doing this.
Last edited on
Topic archived. No new replies allowed.