Program Overview
The program performs the following tasks:
- Pass-by-Value Conversion:
- Takes the length in centimeters as input.
- Converts centimeters to meters and kilometers within the function.
- Displays the converted values inside the function.
- Original value in
main
remains unchanged.
- Pass-by-Reference Conversion:
- Takes the length in centimeters as input.
- Converts centimeters to meters and kilometers within the function.
- Updates the original variables in
main
with the converted values. - Original variables reflect the changes after the function call.
C++ Program Implementation
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 36 37 38 39 |
#include <iostream> using namespace std; // Function to convert cm to meters and kilometers using pass-by-value void convertByValue(int centimeters) { double meters = centimeters / 100.0; double kilometers = centimeters / 100000.0; cout << "Inside convertByValue:" << endl; cout << centimeters << " cm = " << meters << " meters" << endl; cout << centimeters << " cm = " << kilometers << " kilometers" << endl; } // Function to convert cm to meters and kilometers using pass-by-reference void convertByReference(int centimeters, double &meters, double &kilometers) { meters = centimeters / 100.0; kilometers = centimeters / 100000.0; cout << "Inside convertByReference:" << endl; cout << centimeters << " cm = " << meters << " meters" << endl; cout << centimeters << " cm = " << kilometers << " kilometers" << endl; } int main() { int cm = 150000; // Example: 150,000 centimeters double meters, kilometers; // Pass-by-Value Conversion cout << "=== Pass-by-Value Conversion ===" << endl; cout << "Before convertByValue: cm = " << cm << endl; convertByValue(cm); cout << "After convertByValue: cm = " << cm << endl << endl; // Pass-by-Reference Conversion cout << "=== Pass-by-Reference Conversion ===" << endl; cout << "Before convertByReference: cm = " << cm << ", meters = " << meters << ", kilometers = " << kilometers << endl; convertByReference(cm, meters, kilometers); cout << "After convertByReference: cm = " << cm << ", meters = " << meters << ", kilometers = " << kilometers << endl; return 0; } |
Explanation
1. Pass-by-Value (convertByValue
Function)
- Definition:
1void convertByValue(int centimeters)
- Parameters: The function receives the length in centimeters as a copy of the actual argument.
- Operation:
- Converts centimeters to meters by dividing by 100.0.
- Converts centimeters to kilometers by dividing by 100,000.0.
- Displays the converted values inside the function.
- Effect:
Since the function uses pass-by-value, the original cm
variable in main
remains unchanged after the function call.
Inside main
:
1 2 3 |
cout << "Before convertByValue: cm = " << cm << endl; convertByValue(cm); cout << "After convertByValue: cm = " << cm << endl << endl; |
- Displays the value of
cm
before and after callingconvertByValue
.
Observes that cm
remains the same after the function call.
Pass-by-Reference (convertByReference
Function)
- Definition:
1void convertByReference(int centimeters, double &meters, double &kilometers)
- Parameters:
centimeters
: Takes the length in centimeters.meters
: Reference to a double variable to store meters.kilometers
: Reference to a double variable to store kilometers.
- Operation:
- Converts centimeters to meters by dividing by 100.0 and updates
meters
. - Converts centimeters to kilometers by dividing by 100,000.0 and updates
kilometers
. - Displays the converted values inside the function.
- Converts centimeters to meters by dividing by 100.0 and updates
- Effect:
- Since the function uses pass-by-reference, the original
meters
andkilometers
variables inmain
are updated with the converted values after the function call.
- Since the function uses pass-by-reference, the original
- Parameters:
Inside main
:
1 2 3 |
cout << "Before convertByReference: cm = " << cm << ", meters = " << meters << ", kilometers = " << kilometers << endl; convertByReference(cm, meters, kilometers); cout << "After convertByReference: cm = " << cm << ", meters = " << meters << ", kilometers = " << kilometers << endl; |
- Displays the values of
cm
,meters
, andkilometers
before and after callingconvertByReference
. - Observes that
meters
andkilometers
are updated with the converted values after the function call.
Sample Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
=== Pass-by-Value Conversion === Before convertByValue: cm = 150000 Inside convertByValue: 150000 cm = 1500 meters 150000 cm = 1.5 kilometers After convertByValue: cm = 150000 === Pass-by-Reference Conversion === Before convertByReference: cm = 150000, meters = 0, kilometers = 0 Inside convertByReference: 150000 cm = 1500 meters 150000 cm = 1.5 kilometers After convertByReference: cm = 150000, meters = 1500, kilometers = 1.5 |
Key Points
- Pass-by-Value:
- The function receives a copy of the actual argument.
- Changes made inside the function do not affect the original variable.
- Useful when you want to ensure that the original data remains unchanged.
- Pass-by-Reference:
- The function receives references (aliases) to the actual arguments.
- Changes made inside the function directly affect the original variables.
- Useful when you need the function to modify the original data or when you want to avoid copying large amounts of data.