C student doesn't understand directions

• 02-16-2005
Boboki
C student doesn't understand directions
"Check the return value from timesTable. If it is less zero then print out an error message to the user."

I don't know what this means :confused:

Does it involve that return(0); thing we have at the end of every function? timesTable is a function. The only return it has in it is the return(0); thing.

I've tried googling this but I keep getting results for java and other languages. I've tried looking in the text book but the index doesn't have anything. I've looked through just about every single example in the book, but I can't find one that looks like it deals with the return value.

• 02-16-2005
InvariantLoop
Code:

`return 0;`
means that the function will return a value at the end when it is finished it doesnt mean the value is 0.

You can have a function return x; or return a+x;

what your sentence means is that, the calculations done by the timesTable function if it is less than 0 it should give an error to the user. In other words your function should return a no-negative number otherwise give an error message.
• 02-16-2005
sean
Quote:

Code:
return 0;

means that the function will return a value at the end when it is finished it doesnt mean the value is 0.
No, this is incorrect. Placing "return 0;" at the end of your program will literally return the value of 0.

Boboki:

When a function returns a value, you can use that function almost like you would use a constant, here are some examples:

Code:

```myVar = myFunc(); /* assigns the return value of myFunc to myVar */ myVar = myFunc() + 1; /* As above, but adds one */ if(myFunc() < 0) printf("Return value is negative"); /* you can also use them in if statements, etc... */```
• 02-16-2005
InvariantLoop
sean, thanks for correcting me but i thought return 0; just means the function has gotten to an end.

also if u have lets say a function called add(); this function is terminated by return 0; then lets say we have another function called display(); so display calls add(); and displays the result of x+y right? so in this case it wont return 0 literally but the result of x+y right?
• 02-16-2005
Rouss
Quote:

Originally Posted by InvariantLoop
sean, thanks for correcting me but i thought return 0; just means the function has gotten to an end.

also if u have lets say a function called add(); this function is terminated by return 0; then lets say we have another function called display(); so display calls add(); and displays the result of x+y right? so in this case it wont return 0 literally but the result of x+y right?

return 0 will return the integer value 0 from the function. Which sucks if
a) your function isn't of type int (double, long, etc probably won't be affected too negatively, but char will have problems) or
b) you want to return a variable value, which can be something other than 0.

If your add function has return 0 at the end.. It will return 0. If it has return x + y at the end, it will return the sum of x and y, which can be 0 in some cases.

A function comes to its end either when it encounters a return or it executes its last instruction.
• 02-16-2005
sean
I'd need to see some actual code. If display() actually tries to send the result of add() to an output function, it would actually output 0 (if in fact add() returned 0). Just calling a function doesn't necesarily put it's return value to use.

To clarify - you can have more than one return statement in a function, but once one is executed, control returns from the function to it's parent function (or the OS if you're in main). It's quite common for a function to have several conditionals containing return statements that return error codes. You'll see a return 0 at the end to signal success. It might not make sense at first, but 0 is the generally accepted return value for success (unless your function is returning the number of bytes process, or something like that as in the case of some input functions). This is why main almost always ends with returning 0 - it tell the OS the program ran successfully.

edit:
Quote:

but char will have problems
I think you're right in this case (not sure, though), but an int is often more compatible with a char than you would think. A char is, after all, nothing more than a numerical value that is interpreted to represent a symbol.
• 02-17-2005
quzah
Why on earth would it have problems? A char is identical to a tiny integer. There's no difference what so ever. Why would a char care if you gave it a value of zero? How do you think you check for a null terminator, if not for zero?

Quzah.
• 02-17-2005
Rouss
Quote:

Originally Posted by quzah
Why on earth would it have problems? A char is identical to a tiny integer. There's no difference what so ever. Why would a char care if you gave it a value of zero? How do you think you check for a null terminator, if not for zero?

Quzah.

You're right, and I know that a char is identical to a tiny int. But I meant it was because they are not necessarily used the same. You normally wouldn't use an int in a char's place or vice versa, emphasis on normally.

Say you have a function of type char, and you put return 0 at the end. On my computer if I tried to print a character returned by that function, it wouldn't print anything, not even a space. So maybe a new student would be confused on why it's not printing anything, whereas with other types, it would print 0. So they have basically the same effect, it's just harder to see in one that the others I guess. I should have just left that line out because really the main problem would be that sometimes you want to return something other than zero.

Sorry for any confusion I caused.
• 02-17-2005
andyhunter
InvariantLoop: It sounds like you are confusing return with what the function does. All return does is stop function execution and passes a value back to the calling entity, whether or not that is the OS. Here is a simple prog that will hopefully clear this up for you:
Code:

```#include <stdio.h> //checkReturn returns a value of type int int checkReturn(int); //display returns a value of type int int display(void); int main(void){     int answer;     printf("\nCalling checkReturn with value of 1");     answer = checkReturn(1);     printf("\nanswer= %d", answer);     printf("\nCalling checkReturn with value of 0");     answer = checkReturn(0);     printf("\nanswer= %d\n", answer);     return 0; } int checkReturn(int x){     if(x){         return display();     }else{         printf("\nNot calling display.");     }     return 0; } int display(void){     printf("\nDisplay called, returning 5");     return 5; }```
• 02-17-2005
Brain Cell
Imagine a function bieng like a blender , and the values you send to that function are pieces of orange. The juice (value) you'll get eventually is what the blender (function) returned.

Here's a diagram based on andyhunter's program to make this clearer :
Code:

```    <---------- 5 -------------   /                        \ [ Main ] ----- 1 ---- > [checkReturn ]                                 \    ^                                 \    \                                   \    5                                   V    \                                 [ Display ]```
This shows the first call to checkReturn. Main sends 1 to checkReturn , checkReturn checks if the value is 0 or 1 and since its 1 it will return display() wich calls display() before returning anything to main , display() returns 5 to checkReturn wich passes it back to main and 5 is finally saved in variable answer.

hope this helps