Thread: Understanding The Use of Return

  1. #1
    Registered User Unee0x's Avatar
    Join Date
    Nov 2011
    Posts
    12

    Understanding The Use of Return

    I am struggling to grasp a firm grip on How and when it is necessary to use return when creating your own functions in C..
    I saw this piece code Yesterday:

    Code:
    #include <stdio.h>
    unsigned int Fac(unsigned int n)
    {
      if((n <= 1))
        return 1;
      return n * Fac(n-1);
    }
    
    
    int main()
    {
      printf("%d\n", Fac(10));
      return 0;
    }
    I do understand that the return type comes first and the argument type comes next in the parentheses, but the way this person used
    return in this function is kind of hard for me to grasp.
    I would like know where I can read all about how to use returns in created functions and the main function, as well as its purpose in c programming..

    Thank You in advance...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For the concept behind the example that you encountered, search the Web for "recursion". However, if you are struggling to understand plain use of return statements, then that topic will be beyond you for now.
    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

  3. #3
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    "return" is used when you have to give a result back to the "caller". So here, the line in printf is "calling" Fac, and giving it the number 10. Fac is doing stuff (as laserlight says - you need to understand some maths and recursion to understand exactly what) and giving the result back to the printf. Lots of functions "return" a value - a result. Not all of them (the ones that return "void" don't), but most of them. And even if it returns something, it doesn't mean you have to do anything with it (i.e. printf returns a value too, but we ignore it because we don't care about it).

    Additionally, INSIDE Fac, it is calling Fac (i.e. itself) with other numbers, and getting the result back from them to do more work on. It's using "recursion" (calling yourself) to break the problem down. It's like a product going through a production line, the first guy gets it, does what he needs, passes it on. The next guy gets the result of his work, does stuff to it, passes it on. Except in this case, the "guy" is the same guy all the time. And when it's finished, Fac "returns" a value back to the caller in main.

    The actual thing it's doing is called a factorial, which is a mathematical thing. Basically, the factorial of 6 is 6 x 5 x 4 x 3 x 2 x 1. The way Fac calls itself just breaks that down into steps so that one call to Fac in main() does all the hard work and Fac just keeps calling itself to get the full answer (here, for example, it would call itself 5 more times). It's basically being told "work out the factorial of 6" and it tells itself "work out the factorial of 5", and THAT guy says to itself "work out the factorial of 4" etc. because they each need to do only one step, once the bottom guy (the one working out the factorial of 1) answers.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

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