Thread: Return Values ( C )

  1. #1
    Registered User Inept Pig's Avatar
    Join Date
    Apr 2002
    Posts
    140

    Return Values ( C )

    If I have the function:

    int something(char *s_ptr)
    {
    run some comparison tests
    IF true
    return (TRUE)
    }

    The returned value then needs to be compared in the program, to allow another loop to start, where is the value returned to and would it be okay to test for it with:

    if (TRUE)
    {
    code bits
    }

    or would this cause another horrible error, like the ones I'm so used to?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You can put the function call to something directly in the if test like so:
    Code:
    if( something( "hello" ) )
    {
        code bits
    }
    Or you can assign the return value to a temporary/local variable in the function calling the something function and then test that variable like so:
    Code:
    int result = something( "hello" );
    if( result )
    {
        code bits
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User Inept Pig's Avatar
    Join Date
    Apr 2002
    Posts
    140

    Talking

    Hey, the man's a genius...

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM