C# usando recursividad con solo un retorno

I have the following recursive function

public int Factorial(int number_to_calculate)
{
    if (StackChanged != null)
    {
        StackChanged(new CustomEventArgs(StackValue, Result));
    }

    System.Threading.Thread.Sleep(wait_time);
    if (number_to_calculate == 0)
    {
        StackValue--;
        return 1;
    }
    else
    {
        StackValue++;
        Result = (number_to_calculate * Factorial(number_to_calculate - 1));
    }

    if (StackChanged != null)
    {
        StackChanged(new CustomEventArgs(StackValue, Result));
    }
    StackValue--;
    System.Threading.Thread.Sleep(wait_time);
    return Result;
}

Apparently my supervisor is not ok with me having 2 returns, but wants the function to be recursive. So I only need one return. I already tried using an accumulator with goto beginning,in order to have only one return, but i need to increment StackValue every time the function calls itself and decrement it when it comes out of recursion. This way I won't know when it comes out.

¿Alguien tiene alguna idea?

preguntado el 31 de julio de 12 a las 13:07

It sounds like there is a strict coding standard, which isn't a bad thing. However my guess is that if your supervisor doesn't like multiple exit points a 'goto' isn't going to fly either. -

yes, please dont use goto's vez. Also, where is Result defined? -

@OnResolve: Even if there isn't a strict coding standard, goto should be avoided. As Gerard says. -

not ok with me having 2 returns, but wants the function to be recursive - as it is now, the function has 2 returns y it is recursive. Recursion in general is not a way to replace two returns. -

He is not saying he wanna replace one with the other, he is saying the function can be recursive, but can't have 2 returns. -

3 Respuestas

En lugar de

if (number_to_calculate == 0)
    {

        StackValue--;

        return 1;

    }...

do

if (number_to_calculate == 0)
    {

       result = 1;

    }

Respondido 31 Jul 12, 13:07

that did it. Apparently leaving StackValue--; there messes with something. Thanks - robertpas

@robertpas - That would be because if you do StackValue-- before you return result thus you would be decrasing StackValue twice not once. Although I hope you actually UNDERSTAND the solution otherwise YOU WILL have a problem. - Sabueso de seguridad

The simplest way to make a function have a single return statement is to store the value to return in a variable, and then return it at the end. So you would convert something like the following:

int myFunc() {
    if (cond)
        return x;
    else
        return y;
}

En algo como esto:

int myFunc() {
    int returnValue;
    if (cond)
        returnValue = x;
    else
        returnValue = y;
    return returnValue;
}

This principle can be applied in general to any method, as long as you take care that when you change a return statement to variable assignment, you have conditions setup so the rest of the code is skipped.

However, a requirement like this is rather arbitrary. It is expected in general with recursion to have multiple return statements.

Respondido 31 Jul 12, 13:07

int Factorial(int number) 
        {
            int result = 1;
            StackValue++;
            if (StackChanged != null)
                StackChanged(new CustomEventArgs(StackValue, Result));

            if (number > 1) 
            {       
                System.Threading.Thread.Sleep(wait_time); 
                result = number * Factorial(number - 1);
            }

            StackValue--;
            if (StackChanged != null)
                StackChanged(new CustomEventArgs(StackValue, Result));

            return result;
        }

Respondido 31 Jul 12, 14:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.