Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm really new at coding and I would like to know why this is happening:
C#
static void Main(string[] args)
{
	int number = 5;

	int result = factorial(number);

	Console.WriteLine(result);
}


public static int factorial(int number)
{
	if (number ==0 )
	{
		return 1;
	}

	else
	{
		return number * factorial(number - 1);
	}
}


What I have tried:

This is a simple code using recursion in order to get the factorial, I understand the "else" statement that is multiplying to get the factorial of the number.

This multiplication repeats itself until we get to number 0. As by default, we have written our return output for zero as 1, the final result would be multiplied by 1. so the

question is: why it gets multiply since I did not use any operator on the if statement?
Posted
Updated 18-May-20 15:54pm
v2

Firstly, to get to understand the behaviour the best way is to use debugger. Place a breakpoint in your Main method and execute the code step by step while investigating the values of the variables. This helps you to understand what happens. For some instructions, have a look at First look at the debugger - Visual Studio | Microsoft Docs[^]

What comes to the actual question, the operator is in the else branch. The last time the recursion is called the number is containing 1 so the recursive call will get 0 as input and return 1.

Since the recursive call is made before the multiplication, each return value from recursion will be multiplied after the all nested recursions, allowing each inner recursion to return the value from the multiplication to the calling level.

But as said, this is nicely seen when going through the execution with the debugger.
 
Share this answer
 
Comments
Member 14836444 18-May-20 20:40pm    
You're a genius!, "the recursive call is made before the multiplication, each return value from recursion will be multiplied after the all nested recursions, allowing each inner recursion to return the value from the multiplication to the calling level.", I asked this question in several places, they all said: read the manual, take a C# course, But you answer is so knowledgeable. Thank you, I really appreciate it!. I wish one day to know as much as you do!. Besides taking a course, how you learn so much?, I'm impressed
BillWoodruff 19-May-20 2:00am    
+5
The multiplication occurs only in the else part.
Take factorial(3) for example:
  • factorial(3) -> 3 * factorial(2)
    • factorial(2) -> 2 * factorial(1)
      • factorial(1) -> 1 * factorial(0)
        • factorial(0) -> 1
      • factorial(1) = 1 * 1 = 1
    • factorial(2) = 2 * 1 = 2
  • factorial(3) = 3 * 2 = 6

You could return 1 by testing whether number is one instead of zero, it would spare a recursion and would not change the final result.

To better understand the process, put a breakpoint on the if line and start a debug session (F5 in Visual Studio). You will be able to execute line by line (F11) and see the program flow.
 
Share this answer
 
Quote:
so the question is: why it gets multiply since I did not use any operator on the if statement?

This is not how it works, the "return 1" is not multiplied in the "if".
The best is to see how recursive code operate, the tool of choice for this is the debugger.
Advice: comparing with the non recursive version may help to understand the difference and how it works.
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
public static int factorial(int number)
{

Console.WriteLine("Im calculating {0} ", number);

if (number == 0)
{
return 1;
}


int f= number * factorial(number - 1);
Console.WriteLine("done! {0} = {1} " ,number,f );

return f;

}


static void Main(string[] args)
{



int number = 5;
int result = factorial(number);
Console.WriteLine(result);


}
}



Thanks everybody for you answers and your help, while debugging and adding writelines it become quite clear. Have a nice and thanks one again.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900