1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <iostream> using namespace std; // Function to calculate the bill using pass-by-value double calculateBillByValue(double price, int quantity) { double bill = price * quantity; return bill; } // Function to calculate the bill using pass-by-reference void calculateBillByReference(double price, int quantity, double &bill) { bill = price * quantity; } int main() { double price; int quantity; double billValue = 0.0, billReference = 0.0; // Input price and quantity cout << "Enter the price of the item: "; cin >> price; cout << "Enter the quantity: "; cin >> quantity; // Calculate bill using pass-by-value billValue = calculateBillByValue(price, quantity); cout << "Bill calculated using pass-by-value: $" << billValue << endl; // Calculate bill using pass-by-reference calculateBillByReference(price, quantity, billReference); cout << "Bill calculated using pass-by-reference: $" << billReference << endl; return 0; } |
Explanation:
- Pass-by-Value (
calculateBillByValue
):- Function Definition:
double calculateBillByValue(double price, int quantity)
- This function receives the
price
andquantity
as copies of the original values. - It calculates the bill by multiplying
price
andquantity
. - The result is returned to the calling function, but the original variables in
main
remain unchanged.
- Function Definition:
- Pass-by-Reference (
calculateBillByReference
):- Function Definition:
void calculateBillByReference(double price, int quantity, double &bill)
- This function also receives
price
andquantity
, but in addition, it receives a reference to the variablebill
. - It calculates the bill by multiplying
price
andquantity
and stores the result directly in thebill
variable, which is passed by reference. - The original
billReference
variable inmain
is modified.
- Function Definition:
- Main Function (
main
):- The program prompts the user to enter the
price
of an item and thequantity
. - It then calculates the bill using both the pass-by-value and pass-by-reference methods.
- Finally, it displays the calculated bills.
- The program prompts the user to enter the
Example Outputs:
Example 1:
Input:
- Price:
20
- Quantity:
5
Output:
1 2 3 4 |
Enter the price of the item: 20 Enter the quantity: 5 Bill calculated using pass-by-value: $100 Bill calculated using pass-by-reference: $100 |
Example 2:
Input:
- Price:
15.5
- Quantity:
3
Output:
1 2 3 4 5 6 7 8 |
Enter the price of the item: 15.5 Enter the quantity: 3 Bill calculated using pass-by-value: $46.5 Bill calculated using pass-by-reference: $46.5 Enter the price of the item: 15.5 Enter the quantity: 3 Bill calculated using pass-by-value: $46.5 Bill calculated using pass-by-reference: $46.5 |
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 themain
function, reflecting the changes outside of the function.