/* * @topic Q00725 2/27/2014 function definition and declaration * @brief demonstrates that once declared, the function can be called from other functions */ #include <iostream> void pause() { //---------------------------------------------- // prevent window from closing before exiting: //---------------------------------------------- char ch; // ch is a variable that holds a character // curious \n is a "line feed" control character, // it moves the cursor to the next line: std::cout << "\nEnter X to Exit: "; std::cin.ignore( 1, '\n' ); // extract and discard characters std::cin.clear(); // this clears cin state std::cin.get( ch ); // this reads a character from the keyboard, // and pauses the program from before exiting } // Function declarations: // A function declaration declares function name, parameters, and // the return type, but does not define what the function actually does // Once declared, the function can be called. bool is_winning_number( int input, int lucky ); int main() { int lucky = 0; for ( ;; ) { // ask user for a number int input; std::cout << "Enter a number: "; std::cin >> input; // if it's a winning number then say "You win!" if ( is_winning_number( input, lucky ) ) { std::cout << "You win!\n"; } else { std::cout << "You lose!\n"; } lucky = input; } pause(); return 0; } // Function definition -- specifies what the function does: bool is_winning_number( int input, int lucky ) { input = input * 2; if ( input > lucky ) return true; return false; }