### Calling functions

I can't seem to get the hang of this. Can anyone tell me what my problem is?
 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````// Lab010Ex1.cpp – Calculate child tax credit with 3 functions // Created by Man-Chi Leung on 10/27/10 #include using namespace std; double readMiles(); double calcKm(); int main(double kilo) { readMiles(); calcKm(); cout<< "The distance in kilometers is: " << kilo << endl; system("pause"); return 0; } double readMiles() { double miles=0; cout<<"What is the distance in miles: "; cin>> miles; return miles; } double calcKm(double miles) { double kilo=0; kilo=miles*1.67; return kilo; } ``````

Compiler Error:
obj\Debug\main.o(.text+0x132)||In function `main':|
C:\Users\Showdon\Desktop\C++\Lab10\lab1004\main.cpp|14|undefined reference to `calcKm()'|
||=== Build finished: 1 errors, 0 warnings ===|
Showdon wrote:
I can't seem to get the hang of this. Can anyone tell me what my problem is?
 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````// Lab010Ex1.cpp ï¿½ Calculate child tax credit with 3 functions // Created by Man-Chi Leung on 10/27/10 #include using namespace std; double readMiles(); double calcKm(); int main(double kilo) { readMiles(); calcKm(); cout<< "The distance in kilometers is: " << kilo << endl; system("pause"); return 0; } double readMiles() { double miles=0; cout<<"What is the distance in miles: "; cin>> miles; return miles; } double calcKm(double miles) { double kilo=0; kilo=miles*1.67; return kilo; }``````

Compiler Error:
obj\Debug\main.o(.text+0x132)||In function `main':|
C:\Users\Showdon\Desktop\C++\Lab10\lab1004\main.cpp|14|undefined reference to `calcKm()'|
||=== Build finished: 1 errors, 0 warnings ===|

Line 9 declares a function prototype that doesn't take a double as parameter as opposed to function definition found in line 31 which does. End result: The body of `double calcKm()` is nowhere to be found.
Your code is invalid in whole. As for the error message then you did not define function double calcKm(); that is the definition double calcKm(double miles) does not corresponds to the declaration. The declaration has no any parameter.
Last edited on
Thanks webJose, but I'm pretty new to programming, anyway you can dumb it up a little for me?
Last edited on
main() doesn't know about the functions that are declared & defined after it. You have to forward declare the functions before main, which he tried to do but did incorrectly. The forward declarations should look like this:

 ``12`` ``````double readMiles(); double calcKm(double miles);``````

As you can see, there's no information about what they do just that they exist and this is what parameters they take and return. If you put those definitions before the main function, it will now understand what they are and it will use them correctly, even though you don't define what they actually do until after main().

The less-good alternative would be to define those functions in their entirety before the main() function.
Last edited on
Excellent! changed it just like you asked and it worked. Heres the code for anyone that comes across the same issue.

 ``123456789101112131415161718192021222324252627282930313233343536373839`` ``````// Lab010Ex1.cpp â€“ Calculate child tax credit with 3 functions // Created by Man-Chi Leung on 10/27/10 #include using namespace std; double readMiles(); double calcKm(double miles); int main() { double kilo=0.0, miles=0.0; miles=readMiles(); kilo=calcKm(miles); cout<< "The distance in kilometers is: " << kilo << endl; system("pause"); return 0; } double readMiles() { double miles=0; cout<<"What is the distance in miles: "; cin>> miles; return miles; } double calcKm(double miles) { double kilo=0; kilo=miles*1.67; return kilo; } ``````
Except that the conversion from miles to km is multiply by 1.609 not 1.67 :D
Topic archived. No new replies allowed.