Review Session 3

Recording

Example

Functions

Why functions?

Function Definition

Function Prototype (or Function Declaration)

Function Call

Function Arguments

Pass by Value

Pass by Reference

Function Return

Return type

Return value

Warning: do what you say you will do  


Practice Exercise 3

Email only the source code for each practice exercise as an email attachment.  There is no due date.  Use "Practice Exercise 3" as the email subject.   Add (a) comment(s) at the top of your program with your name, practice exercise 3, your compiler and operating system.

Complete the following program.  You will need to write two functions.  flipACoin should generate a random number, 1 or 2 and return the string, "heads" if the random number is 1, otherwise, return the string, "tails".  The first argument of the countHeadsAndTails function should be passed by value.  The next two arguments should be passed by reference.

int main()
{
    string ht;
    int heads = 0;
    int tails = 0;
    for (int i = 0; i < 1000000; i++)
    {
        ht = flipACoin();
        countHeadsAndTails(ht,heads,tails);
    }
    cout << "Heads = " << heads << "  Tails = " << tails << endl;
}

The output should look something like this:

Heads = 500198  Tails = 499802