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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
#include <iostream> using namespace std; // Function to calculate gross salary using pass-by-value void calculateGrossSalaryByValue(double basicSalary) { double hra, da, grossSalary; // Calculate HRA and DA based on the basic salary using if-else if (basicSalary < 10000) { hra = basicSalary * 0.2; da = basicSalary * 0.8; } else { hra = basicSalary * 0.25; da = basicSalary * 0.9; } grossSalary = basicSalary + hra + da; cout << "Inside calculateGrossSalaryByValue:" << endl; cout << "Basic Salary: " << basicSalary << endl; cout << "HRA: " << hra << endl; cout << "DA: " << da << endl; cout << "Gross Salary: " << grossSalary << endl; } // Function to calculate gross salary using pass-by-reference void calculateGrossSalaryByReference(double basicSalary, double &grossSalary) { double hra, da; // Calculate HRA and DA based on the basic salary using if-else if (basicSalary < 10000) { hra = basicSalary * 0.2; da = basicSalary * 0.8; } else { hra = basicSalary * 0.25; da = basicSalary * 0.9; } grossSalary = basicSalary + hra + da; cout << "Inside calculateGrossSalaryByReference:" << endl; cout << "Basic Salary: " << basicSalary << endl; cout << "HRA: " << hra << endl; cout << "DA: " << da << endl; cout << "Gross Salary: " << grossSalary << endl; } int main() { double basicSalary = 12000; // Example basic salary double grossSalary; // Calculate gross salary using pass-by-value cout << "=== Pass-by-Value Calculation ===" << endl; calculateGrossSalaryByValue(basicSalary); cout << "After calculateGrossSalaryByValue: Basic Salary = " << basicSalary << endl << endl; // Calculate gross salary using pass-by-reference cout << "=== Pass-by-Reference Calculation ===" << endl; calculateGrossSalaryByReference(basicSalary, grossSalary); cout << "After calculateGrossSalaryByReference: Gross Salary = " << grossSalary << endl; return 0; } |
Explanation
1. Pass-by-Value (calculateGrossSalaryByValue
Function)
- Definition:
1void calculateGrossSalaryByValue(double basicSalary)
- Parameters: Takes the basic salary as input by value.
- Operation:
- The function calculates the House Rent Allowance (HRA) and Dearness Allowance (DA) based on the basic salary using an
if-else
statement:- If the basic salary is less than 10,000, HRA is 20% of the basic salary, and DA is 80%.
- Otherwise, HRA is 25%, and DA is 90%.
- The gross salary is calculated as the sum of the basic salary, HRA, and DA.
- Displays the basic salary, HRA, DA, and gross salary inside the function.
- The function calculates the House Rent Allowance (HRA) and Dearness Allowance (DA) based on the basic salary using an
- Effect:
- Since the function uses pass-by-value, the original
basicSalary
variable inmain
remains unchanged after the function call.
- Since the function uses pass-by-value, the original
2. Pass-by-Reference (calculateGrossSalaryByReference
Function)
- Definition:
1void calculateGrossSalaryByReference(double basicSalary, double &grossSalary)
- Parameters:
basicSalary
: The basic salary of the employee.grossSalary
: A reference to a double variable that stores the gross salary.
- Operation:
- The function calculates HRA and DA similarly to the pass-by-value function.
- The gross salary is calculated and assigned to the
grossSalary
variable. - Displays the basic salary, HRA, DA, and gross salary inside the function.
- Effect:
- Since the function uses pass-by-reference, the
grossSalary
variable is directly updated with the calculated gross salary after the function call.
- Since the function uses pass-by-reference, the
- Parameters:
Sample Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
=== Pass-by-Value Calculation === Inside calculateGrossSalaryByValue: Basic Salary: 12000 HRA: 3000 DA: 10800 Gross Salary: 25800 After calculateGrossSalaryByValue: Basic Salary = 12000 === Pass-by-Reference Calculation === Inside calculateGrossSalaryByReference: Basic Salary: 12000 HRA: 3000 DA: 10800 Gross Salary: 25800 After calculateGrossSalaryByReference: Gross Salary = 25800 |
Key Points
- Gross Salary Calculation:
- The gross salary is the sum of the basic salary, House Rent Allowance (HRA), and Dearness Allowance (DA).
- The HRA and DA are calculated based on conditions:
- If the basic salary is less than 10,000:
- HRA = 20% of the basic salary
- DA = 80% of the basic salary
- Otherwise:
- HRA = 25% of the basic salary
- DA = 90% of the basic salary
- If the basic salary is less than 10,000:
- Pass-by-Value:
- The function receives a copy of the actual argument.
- Changes made inside the function do not affect the original variable.
- Pass-by-Reference:
- The function receives a reference (alias) to the actual argument.
- The
grossSalary
variable is directly modified based on the calculations inside the function.
- If-Else Statement:
- The if-else statement is used to control the calculation of HRA and DA based on the basic salary.