Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › C++ FUNDAMENTALS
A function definition lists a parameter for each value its body needs. In printTicketCost, quantity is an int parameter and unitPrice is another int parameter. Each parameter gives the incoming value a required data type and a name that the function body can use.
void printTicketCost(int quantity, int unitPrice) {
cout << quantity * unitPrice;
}The names quantity and unitPrice belong to the function definition. They describe the two positions that incoming values must fill. The variables tickets and price have not been connected to these parameters until a function call supplies arguments.
In void printTicketCost(int quantity, int unitPrice), which names are the parameters?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
A function call supplies arguments in parentheses. This call supplies tickets as the first argument and price as the second argument, so it provides one value for each parameter in the definition. Both variables have type int, which is compatible with the two int parameters.
int tickets = 3;
int price = 20;
printTicketCost(tickets, price);The call must match the function definition's number and types of parameters. A call with one argument has nothing for one parameter, and a call with three arguments has an extra value. An argument whose type cannot be used for the required parameter also prevents the call from matching this definition.
In printTicketCost(tickets, price), the first argument, tickets, supplies the first parameter, quantity. Its value 3 goes to quantity. The second argument, price, supplies the second parameter, unitPrice. Its value 20 goes to unitPrice, so the body calculates 3 * 20 and prints 60.
printTicketCost(tickets, price);
// position 1: tickets, 3 -> quantity
// position 2: price, 20 -> unitPrice
printTicketCost(price, tickets);
// position 1: price, 20 -> quantity
// position 2: tickets, 3 -> unitPriceThe reversed call still compiles because both arguments are int, so each can be used for either int parameter. C++ does not compare the variable names or understand that price means a price and tickets means a quantity. It simply maps position 1 to position 1 and position 2 to position 2. The reversed call therefore calculates 20 * 3, which is numerically 60 here, while giving the parameters the wrong meanings.
Fix this call so quantity receives 3 and unitPrice receives 20.
printTicketCost(price, tickets);Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
An ordinary value parameter starts with a copy of its argument's value. When the normal call sends tickets to quantity, quantity starts as 3, but it is a separate variable inside the function. Assigning a new value to quantity changes that parameter copy, not the variable tickets that supplied the original value.
void printTicketCost(int quantity, int unitPrice) {
quantity = 5;
cout << quantity * unitPrice;
}
int tickets = 3;
int price = 20;
printTicketCost(tickets, price);
cout << tickets;For this call, quantity receives a copy of 3 and unitPrice receives a copy of 20. The assignment changes quantity from 3 to 5, so the function prints 100. After the function finishes, tickets is still 3 because the assignment affected only the copy held by quantity.