-2

I got this code as an exercise in my school:

#include <stdio.h>

main()
{
    int unknown(int a, int b)
    {
        if (b == 1) 
            return a;
        else 
            return a + unknown(a, b - 1);
    }
    printf("Value = %i", unknown(3, 4));
}

The outcome is "Value=12". I can't seem to understand why. AFAIK it should be a=3, b=4 and then 3+4, right?

Bandreid
  • 2,727
  • 28
  • 47
  • 1
    Write down two columns for `a` and `b`, and work through their start values until you get to the correct answer. – Alex Reynolds Feb 19 '14 at 13:58
  • 5
    What does this have to do with the comma operator? And tell your school that nested functions are not allowed in C. – Crowman Feb 19 '14 at 13:59
  • @PaulGriffiths, kinda relevant... http://meta.stackexchange.com/a/66378/226150 – jonhopkins Feb 19 '14 at 14:00
  • Isn't this kind of off-topic? This seems to be more about elementary mathematics than anything else. – devnull Feb 19 '14 at 14:02
  • 2
    It seems to me more of a lack of understanding of recursion. – jonhopkins Feb 19 '14 at 14:02
  • 3
    This question appears to be off-topic because it is about elementary mathematics and not about programming. – devnull Feb 19 '14 at 14:03
  • 2
    @PaulGriffiths: I was surprised to see a nested function, and even more surprised to see it compiled when I tried it. It's a Gnu extension, apparently. http://stackoverflow.com/a/666593/10077 – Fred Larson Feb 19 '14 at 14:05
  • @devnull & the others; I really wouldn't consider this as *off-topic*... He needed help on understanding recursive functions in programming, which I think [slim](http://stackoverflow.com/a/21882754/2736228) provided really well. An issue being too easy for me and you, shouldn't make it off-topic, if that was the reason. – Utkan Gezer Feb 19 '14 at 15:32
  • @ThoAppelsin Read the comment by AlexReynolds. I would still say that this could be trivially solved by sitting with a pen and paper. – devnull Feb 19 '14 at 15:34
  • @devnull Yes, but hey, I could say that almost every programming question could be trivially solved with a pen and paper, and that shouldn't make those *almost every programming questions* off-topic. I hadn't said anything contradictory to being trivially solvable with a pen and paper anyway... – Utkan Gezer Feb 19 '14 at 15:53
  • @ThoAppelsin excuse me, I didn't realise WHAT the command does, I thought it was a simple (a+a)*(b-1) or something. I thought this was a place to ask questions about programming not a "you don't even know math" insult clique. Sorry I bothered you. – user3328369 Feb 20 '14 at 22:32
  • @user3328369 Well, I should be the last person for you to refer about that. I have been saying the same all along, this type of a question should really be entirely appropriate to ask here. However, apparently some people were ***born*** with all the computational knowledge and have learned recursion during elementary school... – Utkan Gezer Feb 20 '14 at 22:43
  • @ThoAppelsin I must have understood wrong then, sorry about that (lack of coffee). :) – user3328369 Feb 22 '14 at 19:07

3 Answers3

8

The key to this is that unless b==1, unknown() calls unknown() - this is called recursion.

For brevity, I'm going to call the function f instead of unknown:

Some languages present functions like this in a clearer way with pattern matching; the equivalent in an imaginary pattern matching language might be:

f(x,1) := x
f(x,y) := x + f(x,y-1)

And so...

f(3,4) = 3 + f(3, 4-1)
       = 3 + f(3, 3)
       = 3 + ( 3 + f(3, 3-1))
       = 3 + ( 3 + f(3, 2))
       = 3 + ( 3 + ( 3 + f(3, 2 - 1)))
       = 3 + ( 3 + ( 3 + f(3, 1)))
       = 3 + ( 3 + ( 3 + (3)))
       = 12

I guess your homework is to decide what a better name for the function is than "unknown". Once you've decided, note that recursion is not the best way to implement that function unless your language has specific support for a feature called tail recursion optimisation (this might be a topic you want to shelve for later).


Also, others have noted that nested functions are not allowed in C -- even though your particular compiler might handle them. That means that although your program does this:

 int function1() {
     int function2(int x) {
         ...
     }
     int x = function2(3);
 }

... a standard C compiler would not allow it. The normal way is:

 int function1() {
     int x = function2(3);
 }

 int function2(int x) {
     ...
 }
slim
  • 40,215
  • 13
  • 94
  • 127
  • WOW it was way more complicated than I thought. Thank for you very much for your time, you just saved my a$$. :) ps.: im using dev-c++ and we're instructed to make our code as clear as possible using nesting, but just for reading purposes – user3328369 Feb 20 '14 at 22:35
3

Each time a is added to a for b times

a = 3
b = 4

So, four times a gets added which means 4 * 3 = 12

Edit: a added to a, b times. -Dabo

Sakthi Kumar
  • 3,047
  • 15
  • 28
2

Its the multiplication using recursive addition . Output should be 12. Finally function will return to main

3 + 3 + 3 + 3 = 12   

Note: Although you used nested function here and its working, but it is not allowed by C standard. Its a compiler extension.

haccks
  • 104,019
  • 25
  • 176
  • 264