2

when you try to use the following function for the first time it will give you correct answer which gives you the LCM of two numbers ( Least common multiple ). for the second call with new parameters the static variable wont be starting at 1 which is going to give me wrong answer. is there anyway to set it up to 1 before doing his recursive loop ?

int lcm(int a, int b)
{ 
    static int common = 1;

    if (common % a == 0 && common % b == 0)
        return common;

     common++;
     lcm(a, b);
     return common;
}
drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • 2
    Why do you need it to be static? – drescherjm Dec 24 '17 at 19:12
  • 3
    Make it non-static and pass it to `lcm` as a parameter. Even better, replace the recursion with a loop. – HolyBlackCat Dec 24 '17 at 19:12
  • i'm working on big project, just put this simple example to deliver what my question is, and unfortunately, can't the static variable @drescherjm – Nizar Al Mshantaf Dec 24 '17 at 19:18
  • If you want to touch it outside of the function, you have to make it global. If you don't want to accidentally change it, put it as `private` `static` into a class and `friend` functions as needed. – HolyBlackCat Dec 24 '17 at 19:24

2 Answers2

4

There is no built-in language mechanism for resetting function-level static variables. Although you can built your own way of communicating the need to reset the static to its initial value, it would remain a poorly-readable abuse of the language.

In particular, you should never use static variables in recursive functions, because non-reentrant recursive functions are nearly useless. In addition, your recursive implementation is painfully slow, because it tries candidate lcms sequentially one by one. Feeding it two reasonably large prime numbers would result in stack overflow.

Rewrite your recursive lcm in a way that does not use static.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • that's the answer i wanna hear, and stop flaming me for this example xD just put to deliver what i question is. Thank you so much <3 – Nizar Al Mshantaf Dec 24 '17 at 19:24
  • 1
    @NizarAlMshantaf You are welcome! I think I know where the example comes from. Unfortunately, the example is of very low quality. [Here](https://stackoverflow.com/a/44142697/335858) is a much better implementation, which uses recursion to find `gcd`. – Sergey Kalinichenko Dec 24 '17 at 19:30
2

You should not use static variables for such a case. Use it as a parameter:

int lcm(int a, int b, int &common)
{ 
  if (common % a == 0 && common % b == 0)
    return common;

  common++;
  lcm(a, b, common);
  return common;
}

// now you can also overload it to avoid using argument:
int lcm(int a, int b)
{
    int common = 1;
    return lcm(a, b, common);
}

With static variable, you also break the thread-safety of the function. You would have to make sure only one thread is calling it.

EDIT

If you change the function a little bit:

int lcm(int a, int b, int common = 1)
{ 
   if (common % a == 0 && common % b == 0)
      return common;

   return lcm(a, b, ++common);
}

Now you don't need the common to be a reference and it's much simpler.

seleciii44
  • 1,529
  • 3
  • 13
  • 26