-1

Looking at this code which passes values to functions the usual way (by value, not by reference/pointer):

funky(x)
{

   if (x > 0) {

     x -= 1
     funky(x)

     }

    printf("%d," x)
}

int tester()
{

   funky(x passed in as 5)

   return 0

}

What will the displayed output be?

Is int tester being passed in as the value for x at the top, the funky(x) -= 1 code? If it is then would that just print 4? Nothing is being declared as an int or string so I am not sure what will print.

Tushar Shahi
  • 16,452
  • 1
  • 18
  • 39
  • Why don't you run it and see what the result is (after fixing it to be real code so that it will actually compile)? – kaylum Jun 17 '21 at 05:38
  • 2
    Did you successfully compile that? I doubt it. If you are new to coding you should first build some experience with following tutorials until you can yourself create simple HelloWorl-like programs. Then you can spot the parts of the shown code which will trigger errors, the missing `; `s and the comment inside the `()` of one of the function calls. Then, and only then, not earlier, you should read up on "recursion". Which is absolutely not a topic on the beginners level which you seem to be on. – Yunnosch Jun 17 '21 at 05:39
  • A short answer that may help you understand how recursive functions would is [Recursion flow in c language and how the output is printed](https://stackoverflow.com/a/54362604/3422102) Best way to understand the function is to pull out an 8.5x11 sheet of paper and track the variable values as the recursive calls are made. Just write out what happens for function call, then recursive call 1, recursive call 2, etc.. And remember, at the end, when the recursion starts to *Unwind*, control returns at the point the recursive call was made. – David C. Rankin Jun 17 '21 at 05:42
  • If you want to discuss the output of some piece of code, please provide a [mre] of it and show the output you get, then discuss it and ask about the parts you do not get. Discussing non-compile codes behaviour is impossible. And "Pleae predict the behaviour of this code." is not a StackOverflow question even if the code can be run. – Yunnosch Jun 17 '21 at 05:43
  • @DavidC.Rankin You are right. But I somehow doubt that OP is even aware of recursion being involved. – Yunnosch Jun 17 '21 at 05:45
  • Run your Question here https://www.onlinegdb.com/online_c_compiler I'm telling you mate, you might copy the quiz question wrong. – Æthenwulf Jun 17 '21 at 05:52
  • 1
    I wonder whether the syntax errors are an indicator of bad code on the quiz site, or intentional defense against people just copying the code somewhere to get the output without thinking.... – Yunnosch Jun 17 '21 at 06:03
  • @Yunnosch you're probably right about it being intentional defense. But I'm not sure this even qualifies as recursive - yes the function calls itself, but it's missing an important attribute that makes recursive functions valuable. – Mark Ransom Jun 18 '21 at 18:07

1 Answers1

1

I decided to help you with your learning by providing you with actually compilable code which, I am convinced, will further exactly the same learning goals as what you are currently studying. I will of course not provide you with the output of the shown code (or the shown code changed to be compilable). For two reasons. One: I don't want to do the quiz for you. Two: I want to help you learn, not hinder you.

So this code, if compiled and run:

#include <stdio.h>

int funky(int x)
{
   printf("in  %d, ", x);
   if (x > 0)
   {
     x -= 1;
     printf("mid %d, ", x);
     funky(x);
   }
   printf("out %d, ", x);
}

int main()
{
   funky(/* x passed in as */ 5);
   return 0;
}

will get you the hopefully enlightening output:

in  5, mid 4, in  4, mid 3, in  3, mid 2, in  2, mid 1, in  1, mid 0, in  0,
out 0, out 0, out 1, out 2, out 3, out 4, 

It supports your understanding by making obvious which output is caused by which code part. I added a second and third output to help you understand the recursion trickery used in the quiz code. And I added a newline to the output, simply for readability here. (Side effect of intentionally not giving the character precise output here...)

With the syntax fixes in the code I intend to answer the side questions in your post, while the real puzzle is clarified by the visualisation of the relevant values before and after the recursive call.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54