Chap06-functions - This program has two functions main and...

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
// This program has two functions: main and displayMessage #include <iostream> using namespace std; //***************************************** // Definition of function displayMessage * // This function displays a greeting. * //***************************************** void displayMessage() { cout << "Hello from the function displayMessage.\n" ; } int main() { cout << "Hello from main.\n" ; displayMessage(); // Call displayMessage cout << "Back in function main again.\n" ; return 0; } -------------------------------------------------------------------------------------------------------------------------------- // The function displayMessage is repeatedly called from within a loop. #include <iostream> using namespace std; //***************************************** // Definition of function displayMessage * // This function displays a greeting. * //***************************************** void displayMessage() { cout << "Hello from the function displayMessage.\n" ; } int main() { cout << "Hello from main.\n" ; for ( int count = 0; count < 5; count++) displayMessage(); // Call displayMessage cout << "Back in function main again.\n" ; return 0; } -------------------------------------------------------------------------------------------------------------------------------- // This program has three functions: main, deep, and deeper #include <iostream> using namespace std; //*************************************** // Definition of function deeper * // This function displays a message. *
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
//*************************************** void deeper() { cout << "I am now inside the function deeper.\n" ; } //*************************************** // Definition of function deep * // This function calls function deeper. * //*************************************** void deep() { cout << "I am now inside the function deep.\n" ; deeper(); // Call function deeper cout << "Now I am back in deep.\n" ; } int main() { cout << "I am starting in function main.\n" ; deep(); // Call function deep cout << "Back in function main again.\n" ; return 0; } -------------------------------------------------------------------------------------------------------------------------------- // This program has three functions: main, first, and second. // It uses function prototypes. #include <iostream> using namespace std; // Function prototypes void first(); void second(); int main() { cout << "I am starting in function main.\n" ; first(); // Call function first second(); // Call function second cout << "Back in function main again.\n" ; return 0; } //********************************************* // Definition of function first * // This function displays a message. * //********************************************* void first() { cout << "I am now inside the function first.\n" ; } //*********************************************
Image of page 2
// Definition of function second * // This function displays a message. * //********************************************* void second() { cout << "I am now inside the function second.\n" ; } -------------------------------------------------------------------------------------------------------------------------------- // This program demonstrates a function with a parameter. #include <iostream> using namespace std; // Function prototype void displayValue( int ); int main() { cout << "I am passing 5 to displayValue.\n" ; displayValue(5); // Call displayValue with argument 5 cout << "Now I am back in main.\n" ; return 0; } //********************************************************* // Definition of function displayValue * // It uses an integer parameter whose value is displayed. * //********************************************************* void displayValue( int num) { cout << "The value is " << num << endl; } -------------------------------------------------------------------------------------------------------------------------------- // This program demonstrates a function with a parameter. #include <iostream> using namespace std; // Function prototype void displayValue( int ); int main() { cout << "I am passing several values to displayValue.\n" ; displayValue(5); // Call displayValue with argument 5 displayValue(10); // Call displayValue with argument 10 displayValue(2); // Call displayValue with argument 2 displayValue(16); // Call displayValue with argument 16 cout << "Now I am back in main.\n" ; return 0; }
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
//********************************************************* // Definition of function displayValue * // It uses an integer parameter whose value is displayed. * //********************************************************* void displayValue( int num) { cout << "The value is " << num << endl; } -------------------------------------------------------------------------------------------------------------------------------- // This program demonstrates a function with three parameters.
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern