How can I add multiple functions into one?

Nick Baker

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;
}
rbaleksandar

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I add multiple dict to one multiple?

From Dev

how can i add multiple actions for one Button

From Dev

how can i add multiple actions for one Button

From Dev

Can I use one route for multiple functions?

From Dev

How can I add regex for this one?

From Dev

How can I add multiple Silverlight libraries with same WCF RIA service to one Silverlight application?

From Dev

How can I add multiple project dependencies to a configuration in one instruction in gradle

From Dev

How can I combine multiple integers into one string and add symbol in between?

From Dev

How Can I Merge Multiple Directories into One

From Dev

How Can I Merge Multiple Directories into One

From Dev

How can I add Multiple WHERE condtions

From Dev

how can I add multiple rectangles to JComponent?

From Dev

How can I add multiple layout to GridView?

From Dev

How can I add Multiple WHERE condtions

From Dev

How can I add two transition transforms but one after one?

From Dev

How can I add two transition transforms but one after one?

From Dev

How can i create from 2 functions just one?

From Dev

How can i create from 2 functions just one?

From Dev

how can i get 2 functions in one ng-click?

From Dev

How can I chain functions and choose which one to execute in Swift?

From Dev

How can I add this assembly and use its functions in Powershell?

From Dev

How can I add two functions together in Python 3?

From Dev

How Can I add Email Verification Functions For WooCommerce

From Dev

JavaScript - How can I add functions from different files to a class?

From Dev

How can I have multiple instances of one controller for multiple templates

From Dev

How can I reuse exception handling code for multiple functions in Python?

From Dev

How can I combine multiple nested Substitute functions in Excel?

From Dev

How can I combine multiple nested Substitute functions in Excel?

From Dev

How can I use Global variable across multiple functions in JavaScript?

Related Related

  1. 1

    How can I add multiple dict to one multiple?

  2. 2

    how can i add multiple actions for one Button

  3. 3

    how can i add multiple actions for one Button

  4. 4

    Can I use one route for multiple functions?

  5. 5

    How can I add regex for this one?

  6. 6

    How can I add multiple Silverlight libraries with same WCF RIA service to one Silverlight application?

  7. 7

    How can I add multiple project dependencies to a configuration in one instruction in gradle

  8. 8

    How can I combine multiple integers into one string and add symbol in between?

  9. 9

    How Can I Merge Multiple Directories into One

  10. 10

    How Can I Merge Multiple Directories into One

  11. 11

    How can I add Multiple WHERE condtions

  12. 12

    how can I add multiple rectangles to JComponent?

  13. 13

    How can I add multiple layout to GridView?

  14. 14

    How can I add Multiple WHERE condtions

  15. 15

    How can I add two transition transforms but one after one?

  16. 16

    How can I add two transition transforms but one after one?

  17. 17

    How can i create from 2 functions just one?

  18. 18

    How can i create from 2 functions just one?

  19. 19

    how can i get 2 functions in one ng-click?

  20. 20

    How can I chain functions and choose which one to execute in Swift?

  21. 21

    How can I add this assembly and use its functions in Powershell?

  22. 22

    How can I add two functions together in Python 3?

  23. 23

    How Can I add Email Verification Functions For WooCommerce

  24. 24

    JavaScript - How can I add functions from different files to a class?

  25. 25

    How can I have multiple instances of one controller for multiple templates

  26. 26

    How can I reuse exception handling code for multiple functions in Python?

  27. 27

    How can I combine multiple nested Substitute functions in Excel?

  28. 28

    How can I combine multiple nested Substitute functions in Excel?

  29. 29

    How can I use Global variable across multiple functions in JavaScript?

HotTag

Archive