Loading lessons...
Multiple Parameters
Multiple Parameters
One parameter is nice; several are normal. Functions commonly take two, three, or more values and combine them.
Declaring several
Inside the parentheses, separate the parameters with commas. You can spread them over several lines for readability:
void myFunction(string fname, int age) {
cout << fname << " Doe. " << age << " years old";
}
- Each parameter keeps its own type and name.
- The commas split them; list them all on one line or one per line.
Calling with commas
Pass one argument per parameter, in the same order:
myFunction("Liam", 3);
myFunction("Jenny", 14);
myFunction("Anja", 30);
- The first argument fills the first parameter, the second fills the second, and so on.
- The call and the definition must agree on the count and types.
Order matters
With void f(string name, int age), calling f(14, "Liam") is wrong - it tries to put an int into a string parameter. The pairing is purely positional: left to right.
TL;DR
- Declare multiple parameters separated by commas, on one line or spread out.
- Pass the same number of arguments, in the same order.
- First argument to first parameter, second to second, and so on.
- Count and types must match the parameter list.