Functions and Conditional Statements
Functions and Conditional Statements
A Conditional Statement in a Function
Introduction
Remember that a function is a section of code where you want to isolate one or more oprations. Some of those operations may require some conditions. You have amny options. At a minimum, in the body of a function, you can use one or more conditional statements.
Practical Learning: Introducing Conditional Statements
#include <iostream>
using namespace std;
double excess = 0.00;
double taxAmount = 0.00;
double grossSalary = 0.00;
bool Summarize();
double GetSalary();
void CalculateTaxAmount();
int main()
{
bool complete = Summarize();
}
void CalculateTaxAmount()
{
/* https://tax.wv.gov/Documents/TaxForms/2020/it140.booklet.pdf
* 1 - Single
* 2 - Head of household
* 3 - Married filing joint
* 5 - Widow[er] with dependent child
* Less than $10,000............3% of the taxable income
* At least – But less than –
* $ 10,000 $25,000 $300.00 plus 4% of excess over $10,000
* $ 25,000 $40,000 $900.00 plus 4.5% of excess over $25,000
* $ 40,000 $60,000 $1,575.00 plus 6% of excess over $40,000
* $ 60,000 $2,775.00 plus 6.5% of excess over $60,000
* */
if (grossSalary < 10000)
{
excess = grossSalary * 3.00 / 100;
taxAmount = grossSalary * 3.00 / 100;
}
else if ((grossSalary >= 10000) && (grossSalary < 25000))
{
excess = ((grossSalary - 10000) * 4.00 / 100);
taxAmount = 300.00 + ((grossSalary - 10000) * 4.00 / 100);
}
else if ((grossSalary >= 25000) && (grossSalary < 40000))
{
excess = ((grossSalary - 10000) * 4.00 / 100);
taxAmount = 900 + ((grossSalary - 25000) * 4.50 / 100);
}
else if ((grossSalary >= 40000) && (grossSalary < 60000))
{
excess = ((grossSalary - 40000) * 6.00 / 100);
taxAmount = 1575 + ((grossSalary - 40000) * 6.00 / 100);
}
else // if (grossSalary >= 60000)
{
excess = ((grossSalary - 60000) * 6.50 / 100);
taxAmount = 2775 + ((grossSalary - 60000) * 6.50 / 100);
}
}
double GetSalary()
{
cout << endl << "Enter the information to prepare the taxes\n";
cout << "Gross Salary: ";
double sal;
cin >> sal;
return sal;
}
bool Summarize()
{
cout << "============================================";
cout << "\n - Amazing DeltaX - State Income Tax -";
cout << "\n--------------------------------------------";
cout << "\n -=- West Virginia -=-";
cout << "\n============================================";
grossSalary = GetSalary();
CalculateTaxAmount();
double netPay = grossSalary - taxAmount;
cout << "\n============================================";
cout << "\n - Amazing DeltaX - State Income Tax -";
cout << "\n--------------------------------------------";
cout << "\n -=- West Virginia -=-";
cout << "\n============================================";
cout << "\nGross Salary: " << grossSalary;
cout << "\n--------------------------------------------";
cout << "\nGross Salary: " << excess;
cout << "\nTax Amount: " << taxAmount;
cout << "\nNet Pay: " << netPay;
cout << "\n============================================";
return true;
}============================================
- Amazing DeltaX - State Income Tax -
--------------------------------------------
-=- West Virginia -=-
============================================
Enter the information to prepare the taxes
Gross Salary: 3748.85
============================================
- Amazing DeltaX - State Income Tax -
--------------------------------------------
-=- West Virginia -=-
============================================
Gross Salary: 3748.85
--------------------------------------------
Gross Salary: 112.465
Tax Amount: 112.465
Net Pay: 3636.38
============================================
Press any key to close this window . . .Conditionally Returning a Value
We are already familiar with the ability for a function to return a value. In some cases, the value you want to return is not a simple constant: It may depend on some condition. To make this happen, you have various options. As one option, in a function, you can create a conditional statement to validate a value. You can then store the result in a variable and, at the end of the function, return that variable.
Practical Learning: Calling a Function that Returns a Value
##include <iostream> using namespace std; double excess = 0.00; double grossSalary = 0.00; bool Summarize(); double GetSalary(); double CalculateTaxAmount(); double CalculateTaxExcess(); int main() { Summarize(); } double CalculateTaxAmount() { /* https://tax.wv.gov/Documents/TaxForms/2020/it140.booklet.pdf * 1 - Single * 2 - Head of household * 3 - Married filing joint * 5 - Widow[er] with dependent child * Less than $10,000............3% of the taxable income * At least – But less than – * $ 10,000 $25,000 $300.00 plus 4% of excess over $10,000 * $ 25,000 $40,000 $900.00 plus 4.5% of excess over $25,000 * $ 40,000 $60,000 $1,575.00 plus 6% of excess over $40,000 * $ 60,000 $2,775.00 plus 6.5% of excess over $60,000 * */ double taxAmount = 0.00; if (grossSalary < 10000) { excess = grossSalary * 3.00 / 100; taxAmount = grossSalary * 3.00 / 100; } else if ((grossSalary >= 10000) and (grossSalary < 25000)) { excess = ((grossSalary - 10000) * 4.00 / 100); taxAmount = 300.00 + ((grossSalary - 10000) * 4.00 / 100); } else if ((grossSalary >= 25000) and (grossSalary < 40000)) { excess = ((grossSalary - 10000) * 4.00 / 100); taxAmount = 900 + ((grossSalary - 25000) * 4.50 / 100); } else if ((grossSalary >= 40000) and (grossSalary < 60000)) { excess = ((grossSalary - 40000) * 6.00 / 100); taxAmount = 1575 + ((grossSalary - 40000) * 6.00 / 100); } else // if (grossSalary >= 60000) { excess = ((grossSalary - 60000) * 6.50 / 100); taxAmount = 2775 + ((grossSalary - 60000) * 6.50 / 100); } return taxAmount; } double CalculateTaxExcess() { double excess = 0.00; if (grossSalary < 10000) { excess = grossSalary * 3.00 / 100; } else if ((grossSalary >= 10000) and (grossSalary < 25000)) { excess = ((grossSalary - 10000) * 4.00 / 100); } else if ((grossSalary >= 25000) and (grossSalary < 40000)) { excess = ((grossSalary - 10000) * 4.00 / 100); } else if ((grossSalary >= 40000) and (grossSalary < 60000)) { excess = ((grossSalary - 40000) * 6.00 / 100); } else // if (grossSalary >= 60000) { excess = ((grossSalary - 60000) * 6.50 / 100); } return excess; } double GetSalary() { cout << endl << "Enter the information to prepare the taxes\n"; cout << "Gross Salary: "; double sal; cin >> sal; return sal; } bool Summarize() { cout << "============================================"; cout << "\n - Amazing DeltaX - State Income Tax -"; cout << "\n--------------------------------------------"; cout << "\n -=- West Virginia -=-"; cout << "\n============================================"; grossSalary = GetSalary(); // Getting a value conditionally returned by a function double taxAmount = CalculateTaxAmount(); // Getting another value conditionally returned by a function double exc = CalculateTaxExcess(); double netPay = grossSalary - taxAmount; cout << "\n============================================"; cout << "\n - Amazing DeltaX - State Income Tax -"; cout << "\n--------------------------------------------"; cout << "\n -=- West Virginia -=-"; cout << "\n============================================"; cout << "\nGross Salary: " << grossSalary; cout << "\n--------------------------------------------"; cout << "\nGross Salary: " << excess; cout << "\nTax Amount: " << taxAmount; cout << "\nNet Pay: " << netPay; cout << "\n============================================"; return true; }
============================================
- Amazing DeltaX - State Income Tax -
--------------------------------------------
-=- West Virginia -=-
============================================
Enter the information to prepare the taxes
Gross Salary: 18755.50
============================================
- Amazing DeltaX - State Income Tax -
--------------------------------------------
-=- West Virginia -=-
============================================
Gross Salary: 18755.5
--------------------------------------------
Gross Salary: 350.22
Tax Amount: 650.22
Net Pay: 18105.3
============================================
Press any key to close this window . . .Topics on Conditionally Returning from a Function
Exiting Early From a Function
One of the goals of a function is to perform an action if a certain condition is met. In fact, by including a condition in a function, you can decide whether the action of a function is worth pursuing or completing. In the body of a function where you are checking a condition, once you find out that a certain condition is not met, you can stop checking the condition and get out of the function. This is done with the return keyword. To apply it, in the body of a conditional statement in a function, once you decide that the condition reaches the wrong outcome, type return and a semicolon. Here is an example:
#include <iostream>
using namespace std;
void RegisterCustomer();
int main()
{
cout << "Water Distribution Company\n";
cout << "================================";
RegisterCustomer();
cout << "\n================================";
}
void RegisterCustomer()
{
cout << "\nTypes of Account";
cout << "\n1 - Residential";
cout << "\n2 - Business";
cout << "\n3 - Government";
cout << "\nEnter Account Type: ";
int type;
cin >> type;
/* In this version of our application, we are processing
* only accounts for persons/families or residences. */
if (type != 1)
{
/* If the employee entered a type other than
* that of a family, don't do nothing. */
return;
}
cout << "\n================================";
cout << "\nCustomer Details";
cout << "\n--------------------------------";
cout << "\nAccount Type: RES - Residential";
}
Here is an example of running the program:
Water Distribution Company ================================ Types of Account 1 - Residential 2 - Business 3 - Government Enter Account Type: 2 ================================ Press any key to close this window . . .
Here is another example of running the program:
Water Distribution Company ================================ Types of Account 1 - Residential 2 - Business 3 - Government Enter Account Type: 2 ================================ Press any key to close this window . . .
In the same way, in a function, you can make various comparisons and, any time you want to stop processing anything in the function, stop the processing by typing return;. Here are examples:
#include <iostream>
using namespace std;
void PrepareWaterBill();
int main()
{
cout << "Water Distribution Company";
cout << "\n=========================================================================================";
PrepareWaterBill();
cout << "\n=========================================================================================\n";
}
void PrepareWaterBill()
{
cout << "\nType of Account";
cout << "\n1 - Residential";
cout << "\n2 - Business";
cout << "\n3 - Government";
cout << "\nEnter Account Type: ";
int type;
cin >> type;
/* In this version of our application, we are processing
* only accounts for persons/families or residences. */
if (type != 1)
{
cout << "\n=========================================================================================";
cout << "\nThe current version of this application "
"processes water bills only for family residences.";
return;
}
cout << "\n=========================================================================================";
cout << "\nEnter the counter reading information";
cout << "\nCounter Reading Start: ";
int counterStart;
cin >> counterStart;
cout << "Counter Reading End: ";
int counterEnd;
cin >> counterEnd;
if (counterStart > counterEnd)
{
cout << "=========================================================================================\n";
cout << "You probaly typed the counter reading values in reverse.\n";
cout << "The value for the counter reading end must be \n";
cout << "higher than the counter reading start.";
return;
}
int gallons = counterEnd - counterStart;
double HCFTotal = gallons * 748.05;
cout << "=========================================================================================";
cout << "\nCustomer Details";
cout << "\n-----------------------------------------------------------------------------------------";
cout << "\nCustomer Name: Seven-Corner Dentist";
cout << "\nAccount Type: RES - Residential";
cout << "\n=========================================================================================";
cout << "\nCounter Reading";
cout << "\n-----------------------------------------------------------------------------------------";
cout << "\nCounter Reading Start: " << counterStart;
cout << "\nCounter Reading End: " << counterEnd;
cout << "\n=========================================================================================";
cout << "\nConsumption";
cout << "\n-----------------------------------------------------------------------------------------";
cout << "\nGallons: " << gallons;
cout << "\nHCF Total: " << HCFTotal;
}
Here is an example of running the program:
Water Distribution Company ========================================================================================= Type of Account 1 - Residential 2 - Business 3 - Government Enter Account Type: 2 ========================================================================================= The current version of this application processes water bills only for family residences. ========================================================================================= Press any key to close this window . . .
Here is another example of running the program:
Water Distribution Company ========================================================================================= Type of Account 1 - Residential 2 - Business 3 - Government Enter Account Type: 1 ========================================================================================= Enter the counter reading information Counter Reading Start: 6162 Counter Reading End: 6144 ========================================================================================= You probaly typed the counter reading values in reverse. The value for the counter reading end must be higher than the counter reading start. ========================================================================================= Press any key to close this window . . .
Here is another example of running the program:
Water Distribution Company ========================================================================================= Type of Account 1 - Residential 2 - Business 3 - Government Enter Account Type: 1 ========================================================================================= Enter the counter reading information Counter Reading Start: 6144 Counter Reading End: 6162 ========================================================================================= Customer Details ----------------------------------------------------------------------------------------- Customer Name: Seven-Corner Dentist Account Type: RES - Residential ========================================================================================= Counter Reading ----------------------------------------------------------------------------------------- Counter Reading Start: 6144 Counter Reading End: 6162 ========================================================================================= Consumption ----------------------------------------------------------------------------------------- Gallons: 18 HCF Total: 13464.9 ========================================================================================= Press any key to close this window . . .
Returning in a Condition
You already know that you can create a conditional statement in a function, store the result in a variable, and then return that variable. You also know that you can early stop the operations in a function once you don't see any reason to continue those operations inside the function. You can combine these two concepts. In a function, if you create a conditional statement that will produce a value for a function, instead of first storing the value in a variable, you can return the value directly where it is produced. As always, you have many options.
Somewhere inside a function, you can create an if condition so that if it produces a True result, you can return a value from there. Another option, which is the most common, consists of creating a series of if and if...else conditions; in which case each section would return its value.
Practical Learning: Conditionally Returning a Value
#include <iostream>
using namespace std;
double grossSalary = 0.00;
bool Summarize();
double GetSalary();
double CalculateTaxAmount();
double CalculateTaxExcess();
int main()
{
Summarize();
}
double CalculateTaxAmount()
{
if (grossSalary < 10000)
{
return grossSalary * 3.00 / 100;
}
else if ((grossSalary >= 10000) and (grossSalary < 25000))
{
return 300.00 + ((grossSalary - 10000) * 4.00 / 100);
}
else if ((grossSalary >= 25000) and (grossSalary < 40000))
{
return 900 + ((grossSalary - 25000) * 4.50 / 100);
}
else if ((grossSalary >= 40000) and (grossSalary < 60000))
{
return 1575 + ((grossSalary - 40000) * 6.00 / 100);
}
else // if (grossSalary >= 60000)
{
return 2775 + ((grossSalary - 60000) * 6.50 / 100);
}
}
double CalculateTaxExcess()
{
if (grossSalary < 10000)
{
return grossSalary * 3.00 / 100;
}
else if ((grossSalary >= 10000) and (grossSalary < 25000))
{
return ((grossSalary - 10000) * 4.00 / 100);
}
else if ((grossSalary >= 25000) and (grossSalary < 40000))
{
return ((grossSalary - 10000) * 4.00 / 100);
}
else if ((grossSalary >= 40000) and (grossSalary < 60000))
{
return ((grossSalary - 40000) * 6.00 / 100);
}
else // if (grossSalary >= 60000)
{
return ((grossSalary - 60000) * 6.50 / 100);
}
}
double GetSalary()
{
cout << endl << "Enter the information to prepare the taxes\n";
cout << "Gross Salary: ";
double sal;
cin >> sal;
return sal;
}
bool Summarize()
{
cout << "============================================";
cout << "\n - Amazing DeltaX - State Income Tax -";
cout << "\n--------------------------------------------";
cout << "\n -=- West Virginia -=-";
cout << "\n============================================";
grossSalary = GetSalary();
// Getting a value conditionally returned by a function
double taxAmount = CalculateTaxAmount();
// Getting another value conditionally returned by a function
double excess = CalculateTaxExcess();
double netPay = grossSalary - taxAmount;
cout << "\n============================================";
cout << "\n - Amazing DeltaX - State Income Tax -";
cout << "\n--------------------------------------------";
cout << "\n -=- West Virginia -=-";
cout << "\n============================================";
cout << "\nGross Salary: " << grossSalary;
cout << "\n--------------------------------------------";
cout << "\nGross Salary: " << excess;
cout << "\nTax Amount: " << taxAmount;
cout << "\nNet Pay: " << netPay;
cout << "\n============================================";
return true;
}============================================
- Amazing DeltaX - State Income Tax -
--------------------------------------------
-=- West Virginia -=-
============================================
Enter the information to prepare the taxes
Gross Salary: 117635
============================================
- Amazing DeltaX - State Income Tax -
--------------------------------------------
-=- West Virginia -=-
============================================
Gross Salary: 117635.000
--------------------------------------------
Gross Salary: 3746.275
Tax Amount: 6521.275
Net Pay: 111113.725
============================================|
|
|||
| Previous | Copyright © 2001-2026, FunctionX | Tuesday 01 October 2025, 16:55 | Next |
|
|
|||