Trouble understanding book example (functions)

Hello all, I am having a hard time understanding this particular program presented in my book. I tried my best to write it out but I don't understand the logic. I am new to functions and we just basically started function this week, thank you.

My logic:

- Ask the user to enter any INTEGER value.
- if (isEven(val)) display is even otherwise display odd.

I would assume let's say the user enters 5. So now in the statement if (isEven(5)) function starts. Then we jump to the function. 5 % 2 == 1, so it skips the status = "true" and immediately goes to status = false". Then it returns status to the main. Because it returns status, and because I entered 5, it returns the "status = false" back to the main function, and makes it immediately go to the else displaying it's odd?

This is what I am having trouble with mainly. My original thinking is this, if the user enters 6, for example, that means 6 % 2 == 0 which is true, so immediately the function stays the "stats = true". So, when returning the status to the main function, that means isEven(val) is true so it display it's even?

Thanks so much. Sorry for the additional comments in the program, those are there for memory.


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
#include <iostream>
using namespace std;

bool isEven(int);

int main()
{
	int val;

	cout << "Enter an integer and I will tell you if it is even or odd: ";
	cin >> val;

	if (isEven(val)) // Remember, connected to function paramter to "isEven".
		cout << val << " is even.\n";
	else
		cout << val << " is odd.\n";
}

bool isEven(int number) // Connected.
{
	bool status;

	if (number % 2 == 0) // Val conneted.
		status = true;
	else
		status = false;
	return status;
}
Last edited on
Booleans return values 1 and 0 which means true and false respectively. What your function does is check if it is divisible by 2, if it is divisible then it's an even, if not it's an odd. 5 % 2 == 1, 5 is not divisible by 2 and status becomes 0 and it returns false. 6 % 2 == 0, 6 is divisible by 2 and status becomes 1 and it returns true.
I understand it returns either true or falser, but I don't see how that's connected to the function. Let's say the user enters "5", so immediately the function checks if 5 % 2 == 0, which is not true, so it might return false. My problem is that I don't get how the main function then produces "is odd" is my main issue. The function returning false I understand, same with true.

Then I believe you are talking about this line:
if (isEven(val)) // This can be rewritten as if(isEven(val) == true) .
If val == 0 it'll go through the if block, else it'll go through the else block.
Last edited on
Hi,

If the user enters 5, then the function returns false. That is, it will return false, not maybe. On line 13 the expression in the if statement becomes false, so the execution jumps to line 15, and line 16 produces the output.

Not sure what you mean by the concept of "connected". C++ code consists of statements and expressions. There are various type of statements, but here we are dealing with a function call and two if else statement. An expression evaluates to a value. With an if statement, the expression number % 2 == 0 is a conditional expression, that is, it evaluates to the concept of true or false, which is the 1 or zero arbwok mentioned. Technically, zero is false, any other value is true. If the expression is true, the code immediately after the if statement (and before the else if , or an else). Otherwise, if there is an else if statement, it's condition is checked, and it's code in the braces is executed. If none of the else if statements are true, then the else statements are executed.

When line 27 is encountered, that value replaces the expression in the if statement on line 13. That is what return means

One thing I would change with the code:You call the function with the argument val, but the function receives a parameter number. I like to name them something similar, so it's obvious they are the same thing, or more accurately they have the same value.

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
34
35
36
37
38
39
#include <iostream>
using namespace std;  // this is ok for now, but not recommended

// function declaration
bool isEven(int number); // It's easier to understand if the parameters have 
                                                 // identifiers ( a name for the variable)
                                                 // we don't have to have const here,
                                                 // but apart from that, the function declaration and definition have to be the same
int main()
{
	int number = 9999; //always initialise to something, even though we get input straight away

	cout << "Enter an integer and I will tell you if it is even or odd: \n";
	cin >> number;

	if (isEven(number))  // calling the  function  "isEven", with argument number
                                       // the return value determines what happens next
        {  //always use braces, even if there is only 1 statement
		cout << val << " is even.\n";  // if true this line is executed
        }
	else {
		cout << val << " is odd.\n";  // if false, this line is executed instead
        }
}

// function definition
bool isEven(const int number) // number is the parameter, a copy of the argument. It's const because we are not
                                                 // going to change it in the function
{
	bool status = false;  // always initialise to something

	if (number % 2 == 0) { // conditional expression evaluated
		status = true;
        }
	else {
		status = false;
        }
	return status;
}


Good Luck !!
Makes much more sense now, thank you!
Topic archived. No new replies allowed.