On Sun, 11 May 2008, at 03:20 (GMT), mdnizdil wrote:
> #include<iostream>
> using namespace std;
>
> double mm(double inches);
> double metre(double mm);
> double feet(double inches);
>
> int main()
> {
> double inches,mm,metre,feet;
>
> cout<<"mm"<<mm<<endl;
> cout<<"metre"<<metre<<endl;
> cout<<"feet"<<feet<<endl;
>
> for(int i=0; i<=100; i+10)
> cout<<i<<" "<<endl;
>
> return 0;
>
> }
>
> double mm(double inches)
> double metre(double mm)
> double feet(double inches)
> {
> double mm,metre,feet,inches;
>
> mm=2.54*inches;
> metre=inches/12;
> feet=mm/1000;
>
> }
A function, very briefly, has this structure:
return_type function_name (parameters, ...)
{
variable_type local_variable, ...;
// do something
return result;
}
When you write (below the main() function):
> double mm(double inches)
> double metre(double mm)
> double feet(double inches)
you're really declaring three different functions, but
writing the code for the last one.
When you write:
> {
> double mm,metre,feet,inches;
>
> mm=2.54*inches;
> metre=inches/12;
> feet=mm/1000;
> }
You're declaring four *local* variables and never
return a value (a double in this case). This means
that once the function is executed, these variables
run out of scope and are no longer accesible by the
rest of the code. In other words, you set values that
are doomed to be lost.
Note also that you're redefining the parameter
*inches*.
Here's an example of a function that converts
tempertature (for simplicity from degrees Kelvin to
degrees Celcius):
double ktoc (double temp_k)
{
return temp_k + 273.15;
}
HTH
Ignacio
Nicht vergessen! Am Sonntag, den 11. Mai ist Muttertag
Geschenkideen, Gedichte & mehr: www.yahoo.de/muttertag