I just wrote a simple code.Since it have no warnings or error attentions from the VC compiler,it TOTALLY doesn't work!
What I wonder is whether the problem is due to the sequence of complying as well as how to figure it out.

------------------
Here is the code

#include<iostream>
using namespace std;
int fac(int n);
void main()
	{
	int m;
	cout<<"Enter a number:\n";
	cin>>m;
	cout<<"\n"<<fac(m);
	}
int fac(int n)
	{
	while(n!=1)
		return n*fac(--n);
	return 1;
	
	}

You do not need a while loop in your function. Think about the steps you need to do to get a factorial. There should be a conditional statment in a secursive function to check if you have reached a base case. There should be a call to the function itself if you have not reached a base case. Generally you do not use a loop in a recursive function.

A recursive function needs to do several things. 1. It has to test to see if it is done (To see if it has reached a base case). 2. It has to call itself. 3. The data that the function looks at should be different for each iteration, otherwise, it will end up in an endless loop. 4. It is a good idea to have some sort of protection to make sure it doesn’t end up in an endless loop.

Take a look at this thread:
http://www.daniweb.com/software-development/cpp/threads/411352

you dont need this line I think.

while(n!=1)

before executing this line you need an if statement

return n*fac(--n);

if n == 0 or 1, return 1,
else

return n*fac(--n);

Having learning related konwledge about recursion ,I come to get aware of how to solve such questions .
Sincerely thanks to those who give me hints in time.

------tiredoy

This question has already been answered. Start a new discussion instead.