AaronMT Posted October 15, 2004 Share Posted October 15, 2004 Hello, for an assignment - I must inlcude functions that do not return a value through the function name. Thus they need pointers. My function contains the following void total_food(int *haddock, int *halibut, int *chips, int *foodtotal) { *foodtotal = ((*haddock * 3.0) + (*halibut * 4.0) + (*chips * 2.00)); } but it will not compile. In main, I have haddock, halibut, chips contain values of user input (how many they chose) am I doing something wrong? Link to comment Share on other sites More sharing options...
0 MEMO.INC Posted October 15, 2004 Share Posted October 15, 2004 (edited) In C++ you must put an & before the variable in the function to return a value don't know in C.. why there are many *??? for example if i want to return A and not the function value I use void FUNCTION_NAME(int &A ) { A=4+5; } this will return the value of "a" to where i call the function in this case A would be 9 after I call the function in the main Edited October 15, 2004 by MEMO.INC Link to comment Share on other sites More sharing options...
0 AaronMT Posted October 15, 2004 Author Share Posted October 15, 2004 I got it to read total_food(double haddock, double halibut, double chips, double *foodtotal) { *foodtotal = ((haddock * 3.0) + (halibut * 4.0) + (chips * 2.00)); } and my call is total_food(haddock, halibut, chips, &foodtotal); ?printf("\nTotal Food ?%.2lf", foodtotal); Now why in the heck wont it accept void before the function header? I emailed my prof, and he told me that it is okay if I just pass everything into the function except foodtotal. Link to comment Share on other sites More sharing options...
0 Andareed Posted October 15, 2004 Share Posted October 15, 2004 @MEMO.INC: You are using references not pointers (similar but distinct). @INFERNO2k: void total_food(double haddock ... should work fine. Just make sure you change both the function definition and (if you have it) the foward declaration. What is the specific compiler error you get? Link to comment Share on other sites More sharing options...
0 MEMO.INC Posted October 16, 2004 Share Posted October 16, 2004 Sorry, I am a n00b at this c++ stuff, I hope i learn soon Link to comment Share on other sites More sharing options...
Question
AaronMT
Hello, for an assignment - I must inlcude functions that do not return a value through the function name. Thus they need pointers.
My function contains the following
void total_food(int *haddock, int *halibut, int *chips, int *foodtotal) { *foodtotal = ((*haddock * 3.0) + (*halibut * 4.0) + (*chips * 2.00)); }but it will not compile.
In main, I have haddock, halibut, chips contain values of user input (how many they chose)
am I doing something wrong?
Link to comment
Share on other sites
4 answers to this question
Recommended Posts