Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › C++ FUNDAMENTALS
A function becomes easier to understand when its required values appear in its parameter list. The caller supplies those values in the function call, so the function does not need to search elsewhere for them. For totalCost, the call totalCost(7, 4) sends 7 to price and 4 to quantity. The caller chooses the inputs, and totalCost depends on the values it receives.
int totalCost(int price, int quantity) {
return price * quantity;
}
int total = totalCost(7, 4);The position of each argument determines which parameter receives it. The first argument, 7, becomes price, and the second argument, 4, becomes quantity. This makes the function call a visible description of what the function needs. A function that reads an outside price or quantity could still produce an answer, but the caller would have to know about those hidden dependencies before the call could be trusted.
During the call totalCost(7, 4), which values do the parameters hold inside the function?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
In int totalCost(int price, int quantity), the int before the function name is the return type. It describes the result that totalCost makes available to its caller, not the incoming arguments. The two int types inside the parentheses describe price and quantity. These are separate parts of the declaration: the parentheses describe what goes in, and the type before the name describes what comes out.
int totalCost(int price, int quantity) {
return price * quantity;
}
int total = totalCost(7, 4);For totalCost(7, 4), price * quantity becomes 7 * 4, which produces 28. The return statement sends that value back to the place where the call was made. The assignment then stores the returned 28 in the variable total. The word int promises that this result is an integer the caller can receive and use.
Returning a value and printing a value are different actions. When totalCost returns 28, the caller decides what to do with it, such as storing it in total and printing total afterward. The function performs the calculation, while the caller controls the result's next use.
int totalCost(int price, int quantity) {
return price * quantity;
}
int total = totalCost(7, 4);
std::cout << total;If totalCost printed 28 inside its body instead, the output would appear, but the call would not provide 28 for the assignment to store in total. Printing is an immediate side effect. Returning is a transfer of a value back to the caller, which can then print that value or use it in another calculation.
Replace the missing return type so that int total = totalCost(7, 4) can store 28.
___ totalCost(int price, int quantity) {
return price * quantity;
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
You can read int totalCost(int price, int quantity) as a compact contract. totalCost requires two integer inputs, the first for price and the second for quantity, and it produces one integer result. The call totalCost(7, 4) follows that contract because it supplies two integer arguments in the required order.
The contract lets the language catch certain mistakes instead of leaving them as hidden dependencies. A call that leaves out one of the required arguments does not provide the inputs totalCost needs. A declaration with no usable integer result cannot support an assignment that expects to store the call's result. Reading outside variables and printing inside the function may hide both problems, while explicit arguments and a return type expose them at the function boundary.