#Recursive Function

1 messages · Page 1 of 1 (latest)

olive jewel
#

Hi guys, i am trying to make a recursive function but I am having some troubles.
If the first if is true several times, calling the function herself over and over again when all of the recursive calls ends and the program reaches the second if of the first "checkFour" call, (the original one) the value of "int dist" should be the same of the arg of the first invoke, right?
For "arg of the first invoke" i mean checkFour(, , THIS ONE);

 void checkFour(int x, int y, int dist)
    {
        int xx = x;
        int yy = y;

        xx++;
        if(!vrf(x, y, xx, yy) && check(xx, yy, dist))
        {
            intt.push_back(getStr(x, y, xx, yy));
            checkFour(xx, yy, dist + 1);
        }
        xx -= 2;

        if(!vrf(x, y, xx, yy) && check(xx, yy, dist))
        {
            intt.push_back(getStr(x, y, xx, yy));
            checkFour(xx, yy, dist + 1);
        }
        xx++;
        yy++;

        if(!vrf(x, y, xx, yy) && check(xx, yy, dist))
        {
            intt.push_back(getStr(x, y, xx, yy));
            checkFour(xx, yy, dist + 1);
        }
        yy -= 2;

        if(!vrf(x, y, xx, yy) && check(xx, yy, dist))
        {
            intt.push_back(getStr(x, y, xx, yy));
            checkFour(xx, yy, dist + 1);
        }
    }```
onyx creekBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

#

Hi guys, i am trying to make a recursive function but I am having some troubles.
If the first if is true several times, calling the function herself over and over again when all of the recursive calls ends and the program reaches the second if of the first "checkFour" call, (the original one) the value of "int dist" should be the same of the arg of the first invoke, right?
For "arg of the first invoke" i mean ```c
heckFour(, , THIS ONE);

```cpp

void checkFour(int x, int y, int dist) {
  int xx = x;
  int yy = y;

  xx++;
  if (!vrf(x, y, xx, yy) && check(xx, yy, dist)) {
    intt.push_back(getStr(x, y, xx, yy));
    checkFour(xx, yy, dist + 1);
  }
  xx -= 2;

  if (!vrf(x, y, xx, yy) && check(xx, yy, dist)) {
    intt.push_back(getStr(x, y, xx, yy));
    checkFour(xx, yy, dist + 1);
  }
  xx++;
  yy++;

  if (!vrf(x, y, xx, yy) && check(xx, yy, dist)) {
    intt.push_back(getStr(x, y, xx, yy));
    checkFour(xx, yy, dist + 1);
  }
  yy -= 2;

  if (!vrf(x, y, xx, yy) && check(xx, yy, dist)) {
    intt.push_back(getStr(x, y, xx, yy));
    checkFour(xx, yy, dist + 1);
  }
}
Loo
subtle nexus
#

Kind of hard to tell without more information

#

What do vrf and check do

#

You're exploring the four adjacent squares, unless you keep track of which squares you've reached already you can easily get into infinite recursion

olive jewel
#

ty for ur help but didnt get your point

#

How vrf or check could change the value of dist?

#

wdym of adjacent squares?

#

ohhh

#

u guessed what I am doing

#

yeah I keep tracked and thats what vrf does

onyx creekBOT
#

@olive jewel Has your question been resolved? If so, run !solved :)

onyx creekBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.