### Void and Return-Value Functions ~HELP~ -_-"

Having some issues with return value function and void functions.... particularly how to call (invoke).

objective: Make a program to have user enter deposit to both Savings and Checking accounts.
make a function with getSavingsBalance(deposite, interestRate, year)
Add a new value-return function for Checking account balance similar to Savings getCheckingBalance(deposite, interestRate, year)
Create a void function displayBalance(year, checkingBalance, savingBalance) to display the savings and checking account balance

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465`` ``````#include #include #include using namespace std; //function prototype double getChecking (int amount); double getSavings (int amount, double rate, int y); void displayBalance (int year, double totalChecking, double totalSavings) int main () { int checkingDeposit = 0; int savingsDeposit = 0; double interestRate = 0.0; double totalChecking = 0.0; double totalSavings = 0.0; int year = 0; cout << "Enter Checking Deposit: "; cin >> checkingDeposit; cout << "Enter Savings Deposit: "; cin >> savingsDeposit; cout << "Rate (in decimal form): "; cin >> interestRate; for (int year = 1; year < 4 ; year += 1) { totalSavings = getSavings (savingsDeposit, interestRate, year); cout << "Year " << year << ": \$ " << totalSavings << endl; totalChecking = getChecking (checkingDeposit); cout << "Year " << year << ": \$ " << totalChecking << endl; } return 0; } // end main function // ****function definitions**** double getSavings (int amount, double rate, int y) { double savings = 0.0; savings = amount * pow((1 + rate), y); return savings; } double getCheckings (int amount) { double checkings = 0.0; checkings += amount; return checkings; } void displayBalance (year, totalChecking, totalSavings) { cout << fixed << setprecision (2); cout << "Year " << year << ": \$ " << displayBalance << endl;endl; } }``````
Last edited on
You call a function by writing the name of the function, and then in brackets, the names of the parameters you wish to pass.

Let's do an example, with your function getCheckings.

Name of the function: getCheckings
Names of parameters to pass it: Let's say there is an int that exists, called beans.

So you would call the function like this:

`getCheckings(beans);`

Calling a function is a really basic thing. Definitely make sure you understand how to call a function; it's the name of the function, and then in brackets, the names of the variables you want to pass it, separated by comma. You must put those parameters in the right order.

Here is how to capture the returned value of that function:

`someVariable = getCheckings(beans);`
This stores the returned value from the function in the variable someVariable. So if the function returned, for example, 17, someVariable now has the value 17.

Function calling is (IMHO) described quite well in http://www.cplusplus.com/doc/tutorial/functions/

 function for Checking account balance similar to Savings getCheckingBalance(deposite, interestRate, year)

Overall, what are these Checking and Saving? How do they differ?
Every account has interestRate, money that has been put in, and time it has been there.
What is fundamentally different between checks and saves?

For fun, lets dissect your
 ``123456`` ``````double getCheckings (int amount) { double checkings = 0.0; checkings += amount; return checkings; }``````

The `checkings += amount;` does the same as `checkings = checkings + amount;`
The checkings was initialized with 0.0. Lets substitute the value into the statement:
`checkings = 0.0 + amount;`
That should be about the same as
`checkings = amount;`
Function then returns the checkings, which is equal to the amount.
One more substitution and then discard the unused variable:
 ``1234`` ``````double getCheckings (int amount) { return amount; }``````

An optimizing compiler will do similar reductions.