3.10. Functions with Multiple ParametersΒΆ

The syntax for declaring and invoking functions with multiple parameters is a common source of errors. First, remember that you have to declare the type of every parameter. For example

void printTime (int hour, int minute) {
  cout << hour;
  cout << ":";
  cout << minute;
}

It might be tempting to write (int hour, minute), but that format is only legal for variable declarations, not for parameters.

Another common source of confusion is that you do not have to declare the types of arguments. The following is wrong!

int hour = 11;
int minute = 59;
printTime (int hour, int minute);   // WRONG!

In this case, the compiler can tell the type of hour and minute by looking at their declarations.

Warning

It is unnecessary and illegal to include the type when you pass variables as arguments! The type is only needed for declaration.

The correct syntax is printTime (hour, minute).

This program shows how the dollar_amount and cent_amount arguments are passed into the printPrice function.

Before you keep reading...

Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.

You have attempted 1 of 4 activities on this page