I understand how to pass variables from functions but I've been stuck on adding multiple functions together? I want to add all of the double values stored in the functions into one function called "totalBillAmount". (navyCreditBill + wfStudentBill +....etc). Also, is there any way to combine the functions even tho they all have different double values? (i.e. combining all the Bills to make ONE function called Bills)
//this is my main file
Budget budget;
budget.introText();
double inputNavyCreditCardBill = budget.navyCreditBill();
double inputWellsStudentLoanBill = budget.wfStudentBill();
double inputSprintPhoneBill = budget.sprintBill();
double carBill = budget.carBill();
double capitalOneCreditCard = budget.capitalCredit();
double fedStudentLoan = budget.fedCreditBill();
double navyPersonalLoan = budget.navyPersonalLoan();
double totalMoneyOnHand = budget.totalMoneyOnHand();
double totalBillAmount =
double moneyRemaining = totalMoneyOnHand - totalBillAmount;
std::cout << std::endl << "Money Remaining: $" << moneyRemaining << std::endl;
double extraMoneyTowardsDebt = moneyRemaining * .7;
std::cout << std::endl << "Extra Money For Debt: $" << extraMoneyTowardsDebt << std::endl;
double moneyFromCreditForGirlFriend = moneyRemaining * .3;
std::cout << "Extra Money From Credit For Girlfriend!: $" << moneyFromCreditForGirlFriend << std::endl;
double extraMoneyForMe = moneyRemaining * .3;
std::cout << "MA MONEY BITCH: $" << extraMoneyForMe << std::endl;
return 0;
}
Let's say you have the following functions:
void func1(int bar1) { ... }
void func2(int bar2) { ... }
void func3(int bar3) { ... }
If you want to combine all of these functions inside a new bigger one all you need to do is pass the arguments that each small function requires through the arguments list of the big one:
void bigFunc(int bar1, int bar2, int bar3) {
...
func1(bar1);
func2(bar2);
func3(bar3);
}
Now in your case you also have return values which makes things a little bit more complicated. Depending on what you want to do with these you can either do the calculation inside the big function and then return a single result or use an array, structure or a more elaborate container to store the separate results.
Since in your case you just want to return a single result it's quite easy. Below you can see an example with a bunch of functions (double sum(double, double, double)
, double pow(double, double)
and double sub(double, double)
). You should be able to adapt this to your scenario:
// Use the arguments list of bigFunc to pass down the arguments that each of the smaller function requires
void bigFunc(double arg1, double arg2, double arg3) {
// Use temporary variables to store the return value of each smaller function
double s = sum(arg1, arg2, arg3);
double p = pow(arg1, arg3);
double sb = sub(arg1, arg2);
// Do whatever you want we the results and return it
return s + p - sb;
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments