# Thread: Issues learning how to call a function

1. ## Issues learning how to call a function

My professor has given an assignment where the objective is to learn how to call a function. The assignment is a program that calculates the future value of a savings account. We are supposed to create a function that calculates the interest value based on inputs from the user. I have created a program that calls a function, but I can't seem to get the function to return anything at all. I'm having trouble assigning a value to variables in the function, and I do not know how to take values inputted by the user in the int main() function and use them in the function that I have created. My professor asked that we introduce the function with a prototype above the int main() function, and put the actual commands for the function below the main. Here is what i have so far, how can I 1. get the function to return anything. 2. use variables inputted by the user in the main in my function, 3. assign values to the variables in the function. Thanks
Code:
```#include <iostream>
using namespace std;
double futureinvestmentvalue (double initialdeposit, double monthlyinterestrate, int years);
int main () {
double initialdeposit, monthlyinterestrate;
int years;
cout <<"This program calculates the future balance of your savings account.\n";
cout <<"Please input the amount of the initial deposit into the account: ";
cin >>initialdeposit;
cout <<"Please input the annual interest rate of the account: ";
cin >>monthlyinterestrate;
cout <<"Please input the amount of time in years the account will be active: ";
cin >>years;
cout<<"The final balance of the account is " <<futureinvestmentvalue<< endl;
return 0;
}
double futureinvestmentvalue (double initialdeposit, double monthlyinterestrate, int years){
return 0;
}
/* ----program output-----

This program calculates the future balance of your savings account.
Please input the amount of the initial deposit into the account: 5
Please input the annual interest rate of the account: 5
Please input the amount of time in years the account will be active: 5
The final balance of the account is 008F11AE
Press any key to continue . . .

--------------------------*/```

2. OK, so
1) Do you know how to define a function?
2) Do you know how to call a function?
3) Do you know how to return a value from a function?

If you do know any of the above, can you demonstrate it by creating some elementary function, calling it and returning a value from it?

3. Originally Posted by Elysia
OK, so
1) Do you know how to define a function?
2) Do you know how to call a function?
3) Do you know how to return a value from a function?

If you do know any of the above, can you demonstrate it by creating some elementary function, calling it and returning a value from it?

I am a raw beginner, this is my 3rd assignment, and first time dealing with defining my own function. In this program, I tried to assign the variable futureinvestmentvalue a value of 2, just seeing if that is what the last Cout statement would return, and I received compiler errors relating to the operator "=". For your second question, as far as I understand it, I believe I successfully called the function in my program, I just can't make the function do anything. 3rd, I do not know how to return a value to a function. Thanks again.

4. Can you show what you've tried? We can't exactly read minds, so if we can't see it, we can't tell what's wrong.
Make an example function.

5. Well you defined the function correctly.

To call a function, you write the function name, followed by the arguments you are passing to the function for it to use in a comma separated parenthesis enclosed list, in the way specified by the prototype and definition.

To return something from a function, instead of writing return 0, write return followed by whatever you want to return.

6. For example, If I replace the function at the end of my program with this:
Code:
```double futureinvestmentvalue (double initialdeposit, double monthlyinterestrate, int years){
futureinvestmentvalue=(initialdeposit*monthlyinterestrate);
}```
I get a compiler error relating to the '=' operator. I need the function I am creating to take the values given to variables in the int main() and use them in the function. So for this function above, I would need the initialdeposit variable to use whatever value was given to it by the cin>> statement in the main function. I'll be away from the computer for a while, and won't be able to reply until later today. I appreciate all of your help with this.

7. Replace "futureinvestmentvalue=" with "return."
To return something, don't assign to the function, use the return keyword.

8. Thanks for helping with that. I am still getting an error where the function is returning an incorrect value. I created this program to show the error I am getting.
Code:
```#include <iostream>
using namespace std;
int main(){
int a,b;
cout<<"Input1: ";
cin>>a;
cout<<"input2: ";
cin>>b;
return 0;
}
return (a+b);
}

/* -----program output-----

Input1: 5
input2: 3
Input1 plus input2 equals: 0032104B
Press any key to continue . . .

--------------*/```
How can I get the function to perform a calculation based on the user's input instead of it returning this error?

Thanks again folks

9. As I said above: "To call a function, you write the function name, followed by the arguments you are passing to the function for it to use in a comma separated parenthesis enclosed list, in the way specified by the prototype and definition."

Code:
`cout<<"Input1 plus input2 equals: "<<addition(a,b)<<endl;`
You could also do:
Code:
`cout<<"Input1 plus input2 equals: "<<addition(b,a)<<endl;`
or:
Code:
`cout<<"Input1 plus input2 equals: "<<addition(b,53)<<endl;`

10. Originally Posted by King Mir
As I said above: "To call a function, you write the function name, followed by the arguments you are passing to the function for it to use in a comma separated parenthesis enclosed list, in the way specified by the prototype and definition."

Code:
`cout<<"Input1 plus input2 equals: "<<addition(a,b)<<endl;`
You could also do:
Code:
`cout<<"Input1 plus input2 equals: "<<addition(b,a)<<endl;`
or:
Code:
`cout<<"Input1 plus input2 equals: "<<addition(b,53)<<endl;`
Oh, I missed your first post, can't believe I did that. This worked, Thank you both for your help with my issue. I really appreciate it.

11. Actually, it wasn't returning an error. It was returning the address of the function.