Thread: How to use return values

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    66

    How to use return values

    Really simple question, can't seem to find an answer. How do I do anything with a return value from a function. For example, I want to check the value of the return value with an if statement, what is the name of that which I should be comparing?

    Code:
    void something (blah, blah2)
    {
      ...some code here...
      return 5;
    }
    
    int main()
    {
      something (5, 7);
      if ( ??? == 5)
        {
         ...some code here...
        }
    }
    Last edited by Furious5k; 02-05-2009 at 06:42 PM.

  2. #2
    Registered User
    Join Date
    Apr 2007
    Posts
    141
    First of all your type signature is wrong for returning an integer. Try
    Code:
    int something (int a, int b) { return(5) ; }
    Second you have to have a variable to receive the output of your function (in most cases).
    Code:
    int y = something(5,7) ;
    if (y == 5)
    {
    
    }
    You should give your C book a read now and then, or even try some of the tutorials.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    Quote Originally Posted by SevenThunders View Post
    First of all your type signature is wrong for returning an integer. Try
    Code:
    int something (int a, int b) { return(5) ; }
    Second you have to have a variable to receive the output of your function (in most cases).
    Code:
    int y = something(5,7) ;
    if (y == 5)
    {
    
    }
    You should give your C book a read now and then, or even try some of the tutorials.
    I was just painting in wide strokes so I forgot to add the types.

    Putting a variable to capture the return value is what I was looking for. Thanks!

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    or
    Code:
    if (something(5, 7) == 5) {
    ...
    }

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