Defining Functions

1) Write the definition of a function isEven, which receives an integer parameter and returns true if the parameter 's value is even, and false otherwise.

So if the parameter 's value is 7 or 93 or 11 the function returns false . But if the parameter 's value is 44 or 126 or 7778 the function returns true .

My Code :

1
2
3
4
5
6
7
bool isEven(int x){
    if (x % 2 == 0) {
        return true;
    } else {
                return false;
        }
}


2) Write the definition of a function isPositive, which receives an integer parameter and returns true if the parameter is positive, and false otherwise.

So if the parameter 's value is 7 or 803 or 141 the function returns true . But if the parameter 's value is -22 or -57, or 0, the function returns false .

My Code:

1
2
3
4
5
6
7
8
9
bool isPositive ( int x ) { 
	if ( x > 0 ) 
	{ 
		return true; 
	} 
	else { 
		return false ; 
	} 
}
Last edited on
1
2
3
4
5
6
7
8
9
bool isPositive ( int x ) { 
	if ( x >= 0 ) 
	{ 
		return true; 
	} 
	else { 
		return false ; 
	} 
}


Please note that 0 can be considered a positive or negative number, but is usually implied it is a positive value.

Last edited on
@SakurasouBusters

Read the problem definition before posting. In the problem definition, 0 is supposed to return false. The OP's code is correct as is and yours is wrong.

This is, by the way, the correct answer. 0 is NOT a positive number. Nor is it a negative number. It should never be considered or implied as a positive number.

There might be something wrong in my head. All I can remember is that we can write +0 and -0, and the compiler accepts both versions.
Last edited on
signed vs. unsigned != positive vs. negative.
Topic archived. No new replies allowed.