• 0

[C] - Functions that do not return values


Question

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

  • 0

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 by MEMO.INC
Link to comment
Share on other sites

  • 0

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

  • 0

@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

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.