Node:Value parameters, Next:, Previous:Parameters in function prototypes, Up:Parameters

Value Parameters

When you are passing data to a function by value, the parameters in the function you are passing the data to contain copies of the data in the parameters you are passing the data with. Let us modify the function main from the last example slightly:

int main()
  int bill;
  int fred = 25;
  int frank = 32;
  int franny = 27;

  bill = calculate_bill (fred, frank, franny);

  fred = 20000;
  frank = 50000;
  franny = 20000;

  printf("The total bill comes to $%d.00.\n", bill);

  exit (0);

As far as the function calculate_bill is concerned, fred, frank, and franny are still 25, 32, and 27 respectively. Changing their values to extortionate sums after passing them to calculate_bill does nothing; calculate_bill has already created local copies of the parameters, called diner1, diner2, and diner3 containing the earlier values.

Important: Even if we named the parameters in the definition of calculate_bill to match the parameters of the function call in main (see example below), the result would be the same: main would print out $84.00, not $90000.00. When passing data by value, the parameters in the function call and the parameters in the function definition (which are only copies of the parameters in the function call) are completely separate.

Just to remind you, this is the calculate_bill function:

int calculate_bill (int fred, int frank, int franny)
  int total;

  total = fred + frank + franny;
  return total;