Thread: what is return?

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    54

    what is return?

    Hello everyone.. I know it's a very stupid question, but I can't understand what exactly is the purpose of "return"?

  2. #2
    Registered User
    Join Date
    Mar 2013
    Posts
    2
    It basically returns the value of a function

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    think of it this way:

    when you get in the car and go somewhere, it's equivalent to calling a function. it would be highly unusual to never come back, so when you come home, you're returning from that function call. sometimes you leave to go buy something from the store. when you come home with your merchandise, that's like returning a value.

    Code:
    /* a place to hang out */
    void aVoidFunction()
    {
      /* we don't need to explicitly return, but just for the sake of conversation, we will */
      return;
    }
    
    /* a place to get integer values */
    int anIntFunction()
    {
      /* because our function is expected to send back a value,
          we have to explicitly tell it what to return */
      return 0;
    }
    
    int main()
    {
      /* go somewhere, just to hang out */
      aVoidFunction();
      /* we came back */
    
      /* go somewhere to get an integer value */
      int someInt = anIntFunction();
      /* we came back with our integer value in hand */
    
      /* main() always must return an integer value
          0 = success */
      return 0;
    }

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    Ok, but return is not visible like printf() or usable, right?

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Sotiris Kaniras View Post
    Ok, but return is not visible like printf() or usable, right?
    that is correct.

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    So, we use it for typical reasons?

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    18
    i dont know what exactly you mean by typical, but it is highly useful when you create functions to perform specific tasks and need a value that the function computes in another part of your program

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    it's also useful for stopping the execution of a function before its natural end. let's say it encounters some sort of problem halfway through, and if you continue, it will cause undesired behavior. you would then return, possibly with an error code as the return value, to avoid continuation in the face of the error.

    something like this:
    Code:
    int someFunction(int aParameter)
    {
      if (aParameter < 3)
      {
        return -1 /* aParameter must be at least 3 */
      }
    
      /*continue function here*/
    
      return 0;
    }

  9. #9
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    Ok.. But can we use "return", in order to store data to a variable? For example, something like:

    Code:
    int func(int a)
    {
         if (a == 5)
             return (a = 2);
         else
              return a;
    }
    What would happen then?

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Why don't you try it and look at the result?

    Bye, Andreas

  11. #11
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    Nothing happened.. then I printed out "a" and it printed what I gave to it.. When I gave to the input 5, shouldn't it print 2?

  12. #12
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Sotiris Kaniras View Post
    When I gave to the input 5, shouldn't it print 2?
    How did you test it?

    Bye, Andreas

  13. #13
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    Code:
    #include <stdio.h>
    
    
    int func(int a)
    {
        if (a == 5)
                 return (a = 2);
        else
            return a;
    }
    
    
    int main(int argc, const char * argv[])
    {
        int a;
    
        scanf("%d", &a);
        func(a);
        printf("%d", a);
    
        return 0;
    }
    input: 5
    output: 5

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Notice that you did not use the return value of func in main.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Dec 2012
    Posts
    54
    How do I do that?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 07-04-2007, 12:20 AM
  2. Replies: 12
    Last Post: 04-07-2007, 11:11 AM
  3. Replies: 6
    Last Post: 04-09-2006, 04:32 PM
  4. Replies: 4
    Last Post: 07-15-2005, 04:10 PM
  5. Return Return Error
    By javacvb in forum C++ Programming
    Replies: 8
    Last Post: 12-16-2003, 04:17 PM