Program to calculate the bill in c++

Explanation:

  1. Pass-by-Value (calculateBillByValue):
    • Function Definition: double calculateBillByValue(double price, int quantity)
    • This function receives the price and quantity as copies of the original values.
    • It calculates the bill by multiplying price and quantity.
    • The result is returned to the calling function, but the original variables in main remain unchanged.
  2. Pass-by-Reference (calculateBillByReference):
    • Function Definition: void calculateBillByReference(double price, int quantity, double &bill)
    • This function also receives price and quantity, but in addition, it receives a reference to the variable bill.
    • It calculates the bill by multiplying price and quantity and stores the result directly in the bill variable, which is passed by reference.
    • The original billReference variable in main is modified.
  3. Main Function (main):
    • The program prompts the user to enter the price of an item and the quantity.
    • It then calculates the bill using both the pass-by-value and pass-by-reference methods.
    • Finally, it displays the calculated bills.

Example Outputs:

Example 1:

Input:

  • Price: 20
  • Quantity: 5

Output:

Example 2:

Input:

  • Price: 15.5
  • Quantity: 3

Output:

Explanation of Output:

  • Pass-by-Value: The function returns the result of the bill calculation to the caller, but it does not alter the original variables.
  • Pass-by-Reference: The function directly modifies the billReference variable in the main function, reflecting the changes outside of the function.