// Mr. Minich
// CMPSC 201
// Ch. 6 Demo Program #2
// January 30, 2000 // Purpose -
to illustrate the use of a function that does return a value but that does not have any parameters.
#include <iostream> using namespace std; int obtainFavoriteNumber(); // obtains user's input of a favorite number // this function does not accept any parameters but it does // return an integer (int) value int main() { int sum = 0; // sum of both favorite numbers int myFavNum = 10; // programmer's favorite number int yourFavoriteNumber = 0; // user's favorite number // these two variables are local variables accessible only within // the main function. cout << "My favorite number is " << myFavNum << endl; yourFavoriteNumber = obtainFavoriteNumber();
// This statement is an assignment statement that makes a call to // the function ObtainFavoriteNumber. That function returns an int // which is then assigned to the local variable YourFavoriteNumber. sum = myFavNum + yourFavoriteNumber; cout << "The sum of our favorite numbers is " << sum << endl; return 0; } // end of main int obtainFavoriteNumber() // This function obtains the user's favorite number.
{ int temp = 0; // a local, temporary variable cout << "Enter your favorite number as an integer: "; cin >> temp; return temp; } // end of obtainFavoriteNumber