Thread: Explanation of return

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    46

    Explanation of return

    if i'm returning an int from a function and I want to return 0...why does this work

    return (0); --> for false

    return a; --> if a equals 0, return is still true and works fine but i don't know why it wouldn't return false instead?

    Also, would return(a); work if a was 0? or would it return false?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    return <value>;
    That just returns whatever value is. It can be the value a variable holds, or an actual constant like you have when you return 0 directly. The only other confusion here I see is with regards to what is true and what is false. Anything that is not zero is considered true in a truth test.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    46
    so basically
    a = 0;

    return 0;
    is different from
    return a;
    ?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There's no difference. One takes the value of a and returns it, the other returns the value 0. So if a holds 0 then it is the value that is returned. If a doesn't hold 0 then of course it will be different.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    46
    but then how would

    int delete (struct data_node **, int);

    if(delete(&first, data)) //so how would this determine if I want to return value of int 0 as in TRUE, or just return 0 for FALSE?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's up to you. You can check for any number.
    Code:
    if( myfunction( ) == somevalue )
    If that function happens to return somevalue then that if check will be true. If it doesn't, it will be false.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    46
    right, but I'm trying to avoid using the "== somevalue" that you have there

    so how do I return false if values from 0 to 100 are true and I still want to use if(myfunction()) without the == part that you have in your statment

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The problem comes from this observation being false:
    return a; --> if a equals 0, return is still true and works fine but i don't know why it wouldn't return false instead?
    As long as a equals zero then the result of the function call equates to false.
    You were mistaken when you thought something showed otherwise.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. explanation
    By aromash in forum C Programming
    Replies: 3
    Last Post: 12-04-2010, 04:22 PM
  2. Replies: 1
    Last Post: 07-04-2007, 12:20 AM
  3. Replies: 12
    Last Post: 04-07-2007, 11:11 AM
  4. Replies: 6
    Last Post: 04-09-2006, 04:32 PM
  5. Replies: 4
    Last Post: 07-15-2005, 04:10 PM