Function calls and the stack
When main calls add_tax, a new stack frame appears on top with the function's own variables. The argument price is a copy of cost: changing it inside the function doesn't touch cost in main. That's pass by value.
When the function returns, its frame disappears and the result lands in total.
#include <stdio.h>
double add_tax(double price) {
double tax = price * 0.1;
price = price + tax;
return price;
}
int main(void) {
double cost = 20.0;
double total = add_tax(cost);
printf("cost %.2f, total %.2f\n", cost, total);
return 0;
}
Output:
cost 20.00, total 22.00
From the lesson: Functions