C student doesn't understand directions

This is a discussion on C student doesn't understand directions within the C Programming forums, part of the General Programming Boards category; "Check the return value from timesTable. If it is less zero then print out an error message to the user." ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    4

    Unhappy 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

    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.


    Please help

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    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.
    When no one helps you out. Call google();

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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... */
    I hope that answers your question.

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    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?
    When no one helps you out. Call google();

  5. #5
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    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.

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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:
    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.
    Last edited by sean; 02-16-2005 at 09:35 PM.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  8. #8
    I like code Rouss's Avatar
    Join Date
    Apr 2004
    Posts
    131
    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.

  9. #9
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    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;
    }
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  10. #10
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    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
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Updating in a sequential file?
    By Ronnyv1 in forum C Programming
    Replies: 1
    Last Post: 03-24-2009, 04:41 PM
  2. Database assignment is Killing me!
    By Boltrig in forum C Programming
    Replies: 2
    Last Post: 11-29-2007, 02:56 AM
  3. LinkList Sorting in C
    By simly01 in forum C Programming
    Replies: 3
    Last Post: 11-25-2002, 12:21 PM
  4. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM
  5. A few tips please...
    By gems in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2002, 02:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21