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 40 | #include <iostream> using namespace std; // Function to calculate area of the circle using pass-by-value void calculateAreaByValue(double radius) { double area = 3.14159 * radius * radius; cout << "Area of the circle (By Value): " << area << endl; } // Function to calculate area of the circle using pass-by-reference void calculateAreaByReference(double &radius, double &area) { area = 3.14159 * radius * radius; cout << "Area of the circle (By Reference): " << area << endl; } int main() { double radius, area; // Label for the goto statement start: cout << "Enter the radius of the circle (must be greater than 0): "; cin >> radius; // Check if the radius is valid if (radius <= 0) { cout << "Invalid input! Radius must be greater than 0." << endl; goto start; // Go back to the start label to re-enter the value } // Calculate area using pass-by-value cout << "=== Pass-by-Value Calculation ===" << endl; calculateAreaByValue(radius); // Calculate area using pass-by-reference cout << "\n=== Pass-by-Reference Calculation ===" << endl; calculateAreaByReference(radius, area); cout << "Area after calculateAreaByReference: " << area << endl; return 0; } |
Explanation
1. Pass-by-Value (calculateAreaByValue
Function)
- Definition:1void calculateAreaByValue(double radius)
- Parameters: Accepts the radius of the circle as a parameter by value.
- Operation:
- The function calculates the area of the circle using the formula
Ï€ * radius^2
, whereπ
is approximated as 3.14159. - Displays the calculated area.
- The function calculates the area of the circle using the formula
- Effect:
- The original value of
radius
remains unchanged after the function call.
- The original value of
2. Pass-by-Reference (calculateAreaByReference
Function)
- Definition:1void calculateAreaByReference(double &radius)
- Parameters: Accepts the radius of the circle as a parameter by reference.
- Operation:
- The function calculates the area of the circle using the same formula as above.
- Displays the calculated area.
- Effect:
- The
radius
variable is passed by reference, so any modifications toradius
within the function would affect the original variable. However, in this case, theradius
is not modified inside the function.
- The
3. Input Validation Using goto
- Label: The
start
label marks the point where the program should return if invalid input is entered. - Condition: The program checks if the entered radius is greater than 0.
- Flow: If the radius is not greater than 0, the program prints an error message and prompts the user to re-enter the radius by jumping back to the
start
label.
Sample Output:
1 2 3 4 5 6 7 8 | Enter the radius of the circle (greater than 0): -5 The radius must be greater than 0. Please try again. Enter the radius of the circle (greater than 0): 3 === Pass-by-Value Calculation === Area of the circle (By Value): 28.2743 === Pass-by-Reference Calculation === Area of the circle (By Reference): 28.2743 |
Key Points
- Area Calculation:
- The area of the circle is calculated using the formula
Ï€ * radius^2
. - The program approximates
Ï€
as 3.14159.
- The area of the circle is calculated using the formula
- Pass-by-Value:
- The function receives a copy of the
radius
. - The original variable is unaffected by changes made inside the function.
- The function receives a copy of the
- Pass-by-Reference:
- The function receives a reference to the
radius
. - Any changes to
radius
inside the function would affect the original variable, though in this case, the radius is not modified.
- The function receives a reference to the
- Input Validation with
goto
:- The
goto
statement is used to ensure the user enters a valid radius (greater than 0). - The program prompts the user to re-enter the radius if the input is invalid.
- The