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

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
64
65
#include <iostream>
#include <iomanip>
#include <cmath>

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)

Your getCheckings looks ... different.

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
1
2
3
4
5
6
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:
1
2
3
4
double getCheckings (int amount)
{
	return amount;
}

An optimizing compiler will do similar reductions.
Topic archived. No new replies allowed.