this is my first year of c++ and i am really confused on functions. if someone could help me with one of my examples to get me on the right track i would really appreciate it.

1. using void printMenu().....i dont understand how to set it up
2. using isEven(int, int).......for this one i need to take in two integers, add them together and print back if the number are even.

thank you

well this is my menu:
cout << "'A' Find if the sum of the two integers entered is even or odd.\n";
cout << "'B' Finds if the number entered is prime.\n";
cout << "'C' Finds the sum between two integers.\n";
cout << "'D' Asks for a year and tells if it is a leap year.\n";
cout << "'E' Displays the menu.\n";
cout << "'Q' Quits the program.\n";
cout << "Please enter a command or a type";
cin >> choice;

i dont understand how to use the printMenu function and i dont get how to set it up

A menu is often a set of choices, like enter + to add 2 numbers , enter * multiple 2 numbers, etc. printMenu() would print out all the options you want the user to have. If your instructor has given you a specific menu to construct, use it. If not, create your own.

void isEven(int, int);

is a function declaration. when the program is compiled it tells the software doing the work that every time you call the function you must send two ints to the function and the function doesn't need to return anything. The function declaration doesn't tell the compiling software what to do within the function, however. That's done inthe function definition. The first line of a function definition

void isEven(int first, int second)
{
}

looks somewhat like a function declaration except that the variables being sent to the function must be named and there is no semicolon at the end of the line. The function body goes within the curly cues and tells the function what to do with first and second.

Edited 5 Years Ago by Lerner: n/a

Since you already have menu code written, it's easy to put it into a function that does (most of) that for you:

// THIS IS NEW CODE (function definition, and open-brace)
void printMenu()
{
// END OF NEW CODE

// THIS IS YOUR CODE (indented)
    cout << "'A' Find if the sum of the two integers entered is even or odd.\n";
    cout << "'B' Finds if the number entered is prime.\n";
    cout << "'C' Finds the sum between two integers.\n";
    cout << "'D' Asks for a year and tells if it is a leap year.\n";
    cout << "'E' Displays the menu.\n";
    cout << "'Q' Quits the program.\n";
// END OF YOUR CODE

// THIS IS NEW CODE (close-brace)
}
// END OF NEW CODE

Feel free to remove the comments, those are just there so you can see what parts are "the function" vs. what you already wrote. I omitted the cin >> ... line because (a) that's not part of printing the menu (it's getting the user's choice, after the menu), and (b) the function is defined void meaning it can't return a value (such as the user's choice). For the first reason, I also omitted the prompt for the user to make one of the choices; it's a bit more of a personal thing, but I see that as being part of getting the user's input, not part of printing the options -- but you can go either way on that.

Then later, instead of the lines we've added to the function, we simply call the function we wrote, and it will output the lines for us:

....
printMenu();
cout << "Please enter a command or a type";
cin >> choice;
...

Note that the two lines I didn't include in the function now simply follow the function call. Does that help with how to write the IsEven() function?

One thing which will help you down the road is to be very particular about function-naming (and variable-naming too) ... a function name doesn't need to be a mile long but it should tell you (or anyone else reading your code) what the function does ... and what the function does should, as a result, match the name. From that perspective, maybe the IsEven() function should be named IsSumEven(). And if the function is going to print the answer and not return anything, maybe it should be called PrintIsSumEven(). Or have it return a value instead:

bool IsSumEven(int first, int second)
{
    // determine if the sum is even,
    // if so:
        return true;
    // otherwise:
        return false;
}

Then you could call it from anywhere else a true/false expression would be used:

if (IsSumEven(a, b)) {
  cout << something;
}
This article has been dead for over six months. Start a new discussion instead.