# How to to write two functions to compute investment value

Page 1 of 1

## 8 Replies - 1889 Views - Last Post: 12 October 2012 - 09:43 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=295342&amp;s=408bd03b8e0d5e10eb7bf955e3d97cdd&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Dus

Reputation: 0
• Posts: 14
• Joined: 27-September 12

# How to to write two functions to compute investment value

Posted 12 October 2012 - 09:09 AM

Hi

I couldn't understand how to do a program using two functions!! However, I must use at least two functions in this project

[Write a function that computes future investment value at a given interest rate for a specified number of years. The future investment is determined using the following formula:

Accumulative Value =
investmentAmount * (1 + MonthlyInterestRate) NumberofYears*12]

```
#include <iostream>
#include<math.h>
#include<iomanip>
using namespace std;

int InvesAmount, years, i, futureValue;
float annualRate,monthlyRate;

float futureV()
{
for (i=1;i<=years;i++)
futureValue=InvesAmount*pow((1+monthlyRate),(i*12));
cout<<"Years"<<setw(16)<<"Future Value\n";
cout<<i      <<setw(16)<<futureValue;
}

int main()
{
cin>>InvesAmount;
cout<< "How much is the interest rate?";
cin>>annualRate;
monthlyRate=(annualRate/100)/12;
cout<<"For how long you want to invest?";
cin>>years;
cout<<"Your future inestment values are  ";
futureV;
}
```

Errors:
Control reaches end of non-function
Expression result unused
No Debug Session

[To repeat, your program should at least consist of two functions, the main function and a function that calculates the balance]

Please I want to understand how I do two functions, I didn't find good tutorials in this subject

thanks

Is This A Good Question/Topic? 0

## Replies To: How to to write two functions to compute investment value

### #2 CTphpnwb

• D.I.C Lover

Reputation: 3273
• Posts: 11,559
• Joined: 08-August 08

## Re: How to to write two functions to compute investment value

Posted 12 October 2012 - 09:15 AM

This should stop the immediate error:
```    cout<<"Your future inestment values are  " <<  futureV();

```

but you're not passing anything to the function, so it's still not going to do what you want.

• Saucy!

Reputation: 6203
• Posts: 23,937
• Joined: 23-August 08

## Re: How to to write two functions to compute investment value

Posted 12 October 2012 - 09:24 AM

There are also my function tutorials, linked in my signature.

### #4 Dus

Reputation: 0
• Posts: 14
• Joined: 27-September 12

## Re: How to to write two functions to compute investment value

Posted 12 October 2012 - 11:46 AM

CTphpnwb, on 12 October 2012 - 09:15 AM, said:

This should stop the immediate error:
```    cout<<"Your future inestment values are  " <<  futureV();

```

but you're not passing anything to the function, so it's still not going to do what you want.

Thank u for ur help and I've read the article, but couldn't get how it works in sophisticated functions.
Could you please explain what do you mean that I am not passing anything?
when I write the function name( futureV() ), is it suppose to call the function?

JackOfAllTrades, on 12 October 2012 - 09:24 AM, said:

There are also my function tutorials, linked in my signature.

Thank you I'll check them

### #5 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,440
• Joined: 14-September 12

## Re: How to to write two functions to compute investment value

Posted 12 October 2012 - 11:59 AM

Basically a function is written like this.

```return_type functionName(parameters if any)
{
//code
//return_value if return_type is not 'void'
}

```

So according to this,your futureV() function is supposed to return a floating point value so it would do you a lot good if you could use a 'return' statement instead of a cout so that your function works correctly.

regards,
Raghav

This post has been edited by raghav.naganathan: 12 October 2012 - 12:00 PM

### #6 Dus

Reputation: 0
• Posts: 14
• Joined: 27-September 12

## Re: How to to write two functions to compute investment value

Posted 12 October 2012 - 12:06 PM

raghav.naganathan, on 12 October 2012 - 11:59 AM, said:

Basically a function is written like this.

```return_type functionName(parameters if any)
{
//code
//return_value if return_type is not 'void'
}

```

So according to this,your futureV() function is supposed to return a floating point value so it would do you a lot good if you could use a 'return' statement instead of a cout so that your function works correctly.

regards,
Raghav

Thank u

the code
```#include <iostream>
#include<math.h>
#include<iomanip>
using namespace std;

int InvesAmount, years, i, futureValue;
float annualRate,monthlyRate;

float futureV()
{
for (i=1;i<=years;i++)
{ futureValue=InvesAmount*pow((1+monthlyRate),(i*12));
cout<<i      <<setw(16)<<futureValue;}
}

int main()
{
cout<<"How much is your investment amount?";
cin>>InvesAmount;
cout<< "How much is the annual interest rate?";
cin>>annualRate;
monthlyRate=(annualRate/100)/12;
cout<<"How many years you want to invest?";
cin>>years;
cout<<"Your future inestment values are\n"<<"Years"<<setw(40)<<"    Future Value\n"<<futureV();

}
```

### #7 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,440
• Joined: 14-September 12

## Re: How to to write two functions to compute investment value

Posted 12 October 2012 - 12:17 PM

Dus, on 13 October 2012 - 12:36 AM, said:

raghav.naganathan, on 12 October 2012 - 11:59 AM, said:

Basically a function is written like this.

```return_type functionName(parameters if any)
{
//code
//return_value if return_type is not 'void'
}

```

So according to this,your futureV() function is supposed to return a floating point value so it would do you a lot good if you could use a 'return' statement instead of a cout so that your function works correctly.

regards,
Raghav

Thank u

Let me illustrate that to you with this example

```float sum()
{
float num1=1.0,num2=2.0,sum_float;
sum_float=num1+num2;
return sum_float;
}

```

Try along the same lines. You can use cout as well but make sure you provide a return statement after you do the cout.

regards,
Raghav

### #8 jimblumberg

Reputation: 4628
• Posts: 14,538
• Joined: 25-December 09

## Re: How to to write two functions to compute investment value

Posted 12 October 2012 - 12:24 PM

@raghav.naganathan
Please find an indentation style you like and use it consistently. Especially when providing answers using your own code. In my opinion proper indentation is a mandatory item for proper program structure.
```float sum()
{
float num1 = 1.0, num2 = 2.0, sum_float;
sum_float = num1 + num2;
return sum_float;
}

```

Jim

This post has been edited by jimblumberg: 12 October 2012 - 12:26 PM

### #9 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,440
• Joined: 14-September 12

## Re: How to to write two functions to compute investment value

Posted 12 October 2012 - 09:43 PM

jimblumberg, on 13 October 2012 - 12:54 AM, said:

@raghav.naganathan
Please find an indentation style you like and use it consistently. Especially when providing answers using your own code. In my opinion proper indentation is a mandatory item for proper program structure.
```float sum()
{
float num1 = 1.0, num2 = 2.0, sum_float;
sum_float = num1 + num2;
return sum_float;
}

```

Jim

Sure Jim, thank you for that suggestion. Will keep that in mind when I am posting next time

regards,
Raghav