Parameters and Arguments
Our functions so far do exactly the same thing every time they are called. But to calculate the area of any rectangle, the function needs to know its width and height. Functions get such values through parameters:
#include <iostream>
#include <string>
void introduce(std::string name) {
std::cout << "My name is " << name << "." << std::endl;
}
int main() {
introduce("Anna");
introduce("Tom");
}This program prints:
My name is Anna.
My name is Tom.In the definition, std::string name in the parentheses is a parameter: a variable that the function can use in its body. Just like with other variables, you write its data type and name, only without a semicolon. In the call, the value in the parentheses, "Anna", is an argument. When you call the function, the argument is stored in the parameter, and then the body runs. On the first call, name is "Anna"; on the second call, it's "Tom".
These two words are easy to mix up, so remember: a parameter is a variable in the definition, an argument is a value in the call.
A function can have several parameters, separated by commas. Each parameter has its own data type, even when the types are the same: int a, int b, not int a, b. The arguments go into the parameters in order: the first argument into the first parameter, the second into the second, and so on:
#include <iostream>
#include <string>
void introduce(std::string name, int age) {
std::cout << "My name is " << name << " and I am " << age << " years old." << std::endl;
}
int main() {
introduce("Anna", 21);
introduce("Tom", 17);
}This program prints:
My name is Anna and I am 21 years old.
My name is Tom and I am 17 years old.C++ checks that the arguments match the parameters. Each parameter needs its argument. If you write only introduce("Anna"); in this program, it doesn't compile:
main.cpp:9:5: error: no matching function for call to 'introduce'
9 | introduce("Anna");
| ^~~~~~~~~
main.cpp:4:6: note: candidate function not viable: requires 2 arguments, but 1 was provided
4 | void introduce(std::string name, int age) {
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.The first line of the error says that the compiler found no function introduce that it could call like this. The line with note explains why: the function needs 2 arguments, but it got only 1.
You get a similar error when you swap the arguments. The call introduce(21, "Anna") would put a number into name, whose type is std::string, so the note below the error says no known conversion from 'int' to 'std::string' (a number can't be turned into text). But when two parameters have the same type, the compiler can't notice the swap, and the function simply works with the swapped values. So always check the order of your arguments.
Now we can solve the problem from the beginning of the lesson: one function that prints the area of any rectangle.
Instructions
Above main, create a function printArea with two parameters of type int, width and height. In its body, print width * height.
In main, call it with the arguments 5 and 6. The program prints 30.
Call printArea two more times, for the other two rectangles from the beginning of the lesson: with the arguments 10 and 5, and then 1 and 5. The program then prints 30, 50 and 5, just like the long program at the beginning, but now each rectangle takes a single line.