Multiple Arguments

A function can have more than one argument. In the function call, you just separate the arguments with commas:

n_chars('R', 25);

This passes two arguments to the function n_chars(), which will be defined shortly.

Similarly, when you define the function, you use a comma-separated list of parameter declarations in the function header:

void n_chars(char c, int n)  // two arguments

This function header states that the function n_chars() takes one type char argument and one type int argument. The parameters c and n are initialized with the values passed to the function. If a function has two parameters of the same type, you have to give the type of each parameter separately. You can’t combine declarations the way you can when you declare regular variables:

void fifi(float a, float b)  // declare each variable separately
void fufu(float a, b)        // NOT acceptable

As with other functions, you just add a semicolon to get a prototype:

void n_chars(char c, int n); // prototype, style 1

As with single arguments, you don’t have to use the same variable names in the prototype as in the definition, and you can omit the variable names in the prototype:

void n_chars(char, int);     // prototype, style 2

However, providing variable names can make the prototype more understandable, particularly if two parameters are the same type. Then the names can remind you which argument is which:

double melon_density(double weight, double volume);

Listing 7.3 shows an example of a function with two arguments. It also illustrates how changing the value of a formal parameter in a function has no effect on the data in the calling program.

Listing 7.3. twoarg.cpp


// twoarg.cpp -- a function with 2 arguments
#include <iostream>
using namespace std;
void n_chars(char, int);
int main()
{
    int times;
    char ch;

    cout << "Enter a character: ";
    cin >> ch;
    while (ch != 'q')        // q to quit
    {
        cout << "Enter an integer: ";
        cin >> times;
        n_chars(ch, times); // function with two arguments
        cout << " Enter another character or press the"
                " q-key to quit: ";
           cin >> ch;
    }
    cout << "The value of times is " << times << ". ";
    cout << "Bye ";
    return 0;
}

void n_chars(char c, int n) // displays c n times
{
    while (n-- > 0)         // continue until n reaches 0
        cout << c;
}


The program in Listing 7.3 illustrates placing a using directive above the function definitions rather than within the functions. Here is a sample run:

Enter a character: W
Enter an integer: 50
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
Enter another character or press the q-key to quit: a
Enter an integer: 20
aaaaaaaaaaaaaaaaaaaa
Enter another character or press the q-key to quit: q
The value of times is 20.
Bye

Program Notes

The main() function in Listing 7.3 uses a while loop to provide repeated input (and to keep your loop skills fresh). Note that it uses cin >> ch rather than cin.get(ch) or ch = cin.get() to read a character. There’s a good reason for this. Recall that the two cin.get()functions read all input characters, including spaces and newlines, whereas cin >> skips spaces and newlines. When you respond to the program prompt, you have to press Enter at the end of each line, thus generating a newline character. The cin >> ch approach conveniently skips over these newlines, but the cin.get() siblings read the newline following each number entered as the next character to display. You can program around this nuisance, but it’s simpler to use cin as the program in Listing 7.3 does.

The n_chars() function takes two arguments: a character c and an integer n. It then uses a loop to display the character the number of times the integer specifies:

while (n-- > 0)         // continue until n reaches 0
      cout << c;

Notice that the program keeps count by decrementing the n variable, where n is the formal parameter from the argument list. This variable is assigned the value of the times variable in main(). The while loop then decreases n to 0, but, as the sample run demonstrates, changing the value of n has no effect on times. Even if you use the name n instead of times in main(), the value of n in main() is unaffected by changes in the value of n in n_chars().

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.172.200