0

In my class in college, my teacher did not really explain well how this code would output . I did not come across this kind of example when I searched the site, and I would like to share it with you. I have got one more question,is this function working like a recursive function too?

#include <stdio.h>
void F(int *a, int b)
{
    (*a)--;b+=2;
    if(*a+b<10)
    {
        printf("\n%d %d",*a,b);
        return;
    }
    (*a)--;b--;
    printf("\n%d %d",*a,b);
    F(&b,*a);
    (*a)++;b++;
    printf("\n%d %d",*a,b);
    return;
}
main()
{
    int b=5;
    F(&b,b);
    printf("\n%d",b);
    return 0;
}
dozgunay
  • 31
  • 8
  • 1
    Yes, it calls itself, therefore it's recursive. – StoryTeller - Unslander Monica Apr 15 '17 at 19:39
  • 1
    A recursive function is a function that calls itself. Does your function call itself? So, is it recursive? – ForceBru Apr 15 '17 at 19:39
  • @ForceBru Yes,it is a recursive function. – dozgunay Apr 15 '17 at 19:44
  • @dozgunay, you see, now you can answer your question yourself:) – ForceBru Apr 15 '17 at 19:45
  • Your teacher's aim is to show you that the difference between passing by pointer vs. passing by value? b's final value will never be changed independent of the F function. – avatli Apr 15 '17 at 19:55
  • Yes it's recursive. The purpose of `return` is to stop executing the function and pass back control to the caller, just as is true in every other C like language. – David Hoelzer Apr 15 '17 at 19:58
  • 3
    If your teacher is teaching `main()` instead of `int main(void)` or `int main(int argc, char *argv[])`, and printing leading newlines instead of trailing newlines, then the best thing will be to humour him/her and be proactive on sites such as this ;) – Weather Vane Apr 15 '17 at 20:04
  • Also, understand that while recursive functions have their place, if you can accomplish the same task without recursion you are often better off (especially when the function will call itself many times before exiting on the test clause) Why? Each recursive call is a separate and distinct function call that sets up an independent function stack. That can cause the use of a large amount of resources and will often be less efficient than a straight-forward procedural approach. Use recursion when it offers a better solution, otherwise, look for a procedural one. – David C. Rankin Apr 15 '17 at 20:14
  • She solved that question like opening boxes in main and another addresses from function calling point but I did not understand after the output is 3 6 \n 4 4\n 3 6 – dozgunay Apr 15 '17 at 20:21
  • Here are a few helpful links: [**Recursion in C**](http://www.cprogramming.com/tutorial/c/lesson16.html), [**C Programming Recursion**](https://www.programiz.com/c-programming/c-recursion), [**Recursive Functions**](https://www.cs.umd.edu/class/fall2002/cmsc214/Tutorial/recursion2.html) (pay attention to the *tail recursion* distinction) – David C. Rankin Apr 15 '17 at 20:37
  • @DavidC.Rankin I am very thankful that for your attention – dozgunay Apr 15 '17 at 20:44
  • Possible duplicate of [Is this function recursive even though it doesn't call itself?](http://stackoverflow.com/questions/26897208/is-this-function-recursive-even-though-it-doesnt-call-itself) – ivan_pozdeev Apr 16 '17 at 18:30

1 Answers1

0

As to your questions:

  1. Recursion is a programming technique that allows the programmer to express operations in terms of themselves. In C, this takes the form of a function that calls itself. The given functions void F(int *a, int b) calls to itself in the function body ==> So it is a recursion.
  2. return - Terminates current function and returns specified value to the caller function. The function F returns void which means nothing, just terminates and returns to the caller. Since in this specific example the return statement appears just before the end of the function, it has really no meaning. Without the return (in this case) the functionality remains the same.
Alex Lop.
  • 6,810
  • 1
  • 26
  • 45