function

im new to c++ and im trying to learn the function together with passing the parameter. ive tried doing a question on coding a simple currency converter using function. but i cant seems to find the problem. pls help meee

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double convert();

int main()
{
double rm;
double amount;
cout << "Enter the amount that you want to convert" << endl;
cin >> amount;
const double usd = 4.0;
double convert(double amount , const double usd);
cout << " RM : " << rm << endl;
return 0;
}

double convert( double amount, double usd, double rm)
{

rm = (amount*usd);
return rm;
}
Last edited on
double convert(double amount , const double usd);
This is not how to call a function.


This is:
rm = convert(amount, usd);


Your declaration of the function:
double convert();
does not match your attempt to call it:
double convert(double amount , const double usd);
and neither of those match the function definition:
double convert( double amount, double usd, double rm)

They have to match.
Last edited on
When a function is defined, you have to specify the types of the arguments, as you correctly did.
 
double convert( double amount, double usd, double rm)

this means you have a function named convert that will return a double and will take 3 doubles as arguments, which will be named amount, usd and rm INSIDE the function.
But when it is CALLED you don't have to specify the type again:
1
2
convert(15, 20, 32); //this is a valid call
double result = convert(15, 20, 32); //this is a call that saves the result inside a variable named result. 


The point is you are missing basics here. The "answer" of a function is your return value, so it doesn't have to be in the signature.
Correct:

1
2
3
4
double convert( double amount, double usd)
    {
    return(amount*usd);
    }

Also note, you seem to think you return the variable, since you later use "rm" in the main. It's not like that. The variables in the signature and in the function exist only inside the function, their "names" have no meaning outside.
Hence, you have to assign the VALUE returned by your function, to a new variable in the place you call said function.
1
2
3
4
5
6
7
8
9
int main()
    {
    double rm, amount;
    const double usd = 4.0;
    cin >> amount;

    rm = convert(amount, usd);
    cout << "RM: " << rm << endl;
    }

Even if you created a variable named "rm" inside your function, it wouldn't change anything in your main. You have to assign the return value of the function to something in your main.

There are ways to do it in another way, but since you seem to be missing the basics i don't want to give you even more troubles understanding.

You might want to do some search about variables scoping in c++ (which is the same in most languages anyway)
Last edited on
wow thanks guys. my program is running now. thanks again for the tips . ill do my research on the variable scooping again.
Topic archived. No new replies allowed.