beginner question... recursive function
-
i have this code and it works fine.
int fatt(int n)
{
int fattoriale = 1;if(n==0) return fattoriale; else return fattoriale=n\*fatt(n-1);
}
void main(void)
{
printf("%d\n",fatt(10));fflush(stdin); getchar();
}
my question is why it works fine also here ?
int fatt(int n)
{
int fattoriale = 1;if(n==0) return fattoriale; else fattoriale=n\*fatt(n-1);
}
void main(void)
{
printf("%d\n",fatt(10));fflush(stdin); getchar();
}
in this, there aren't the return in the else.... i am using VS2010 and compile in .c . thanks ps: sorry for my english
-
i have this code and it works fine.
int fatt(int n)
{
int fattoriale = 1;if(n==0) return fattoriale; else return fattoriale=n\*fatt(n-1);
}
void main(void)
{
printf("%d\n",fatt(10));fflush(stdin); getchar();
}
my question is why it works fine also here ?
int fatt(int n)
{
int fattoriale = 1;if(n==0) return fattoriale; else fattoriale=n\*fatt(n-1);
}
void main(void)
{
printf("%d\n",fatt(10));fflush(stdin); getchar();
}
in this, there aren't the return in the else.... i am using VS2010 and compile in .c . thanks ps: sorry for my english
In release mode it will create compilation error. It creates compilation error in VS2008. I got compilation error "error C4716: 'fatt' : must return a value". But in debug mode it works as follows. In both case contents of EAX register is used as the output of fatt() function. In both case EAX holds the output value, therefore the two versions works correctly in debug mode. The result of imul is available in eax register and that is the expected result from the fatt function. Following are the disassembly of two versions of fatt() function. First one with return value and next is without return value.
return fattoriale=n\*fatt(n-1);
01182ED2 mov eax,dword ptr [n]
01182ED5 sub eax,1
01182ED8 push eax
01182ED9 call fatt (1181889h)
01182EDE add esp,4
01182EE1 imul eax,dword ptr [n]
01182EE5 mov dword ptr [fattoriale],eax
01182EE8 mov eax,dword ptr [fattoriale]fattoriale=n\*fatt(n-1);
01182F92 mov eax,dword ptr [n]
01182F95 sub eax,1
01182F98 push eax
01182F99 call fatt (11815FAh)
01182F9E add esp,4
01182FA1 imul eax,dword ptr [n]
01182FA5 mov dword ptr [fattoriale],eaxThe multiplication in n*fatt(n-1) is done with imul operation. And its output is available in EAX register. In first version( with return statement), last two instructions perform the following things. Contents of eax is moved to fattoriale and after that the contents of fattoriale is moved to eax( EAX holds the return value, or result of operation). In second version( without return statement) the last mov is not found. But the output of iMul is in EAX register and therefore the output will be available to caller function through EAX register.
-
In release mode it will create compilation error. It creates compilation error in VS2008. I got compilation error "error C4716: 'fatt' : must return a value". But in debug mode it works as follows. In both case contents of EAX register is used as the output of fatt() function. In both case EAX holds the output value, therefore the two versions works correctly in debug mode. The result of imul is available in eax register and that is the expected result from the fatt function. Following are the disassembly of two versions of fatt() function. First one with return value and next is without return value.
return fattoriale=n\*fatt(n-1);
01182ED2 mov eax,dword ptr [n]
01182ED5 sub eax,1
01182ED8 push eax
01182ED9 call fatt (1181889h)
01182EDE add esp,4
01182EE1 imul eax,dword ptr [n]
01182EE5 mov dword ptr [fattoriale],eax
01182EE8 mov eax,dword ptr [fattoriale]fattoriale=n\*fatt(n-1);
01182F92 mov eax,dword ptr [n]
01182F95 sub eax,1
01182F98 push eax
01182F99 call fatt (11815FAh)
01182F9E add esp,4
01182FA1 imul eax,dword ptr [n]
01182FA5 mov dword ptr [fattoriale],eaxThe multiplication in n*fatt(n-1) is done with imul operation. And its output is available in EAX register. In first version( with return statement), last two instructions perform the following things. Contents of eax is moved to fattoriale and after that the contents of fattoriale is moved to eax( EAX holds the return value, or result of operation). In second version( without return statement) the last mov is not found. But the output of iMul is in EAX register and therefore the output will be available to caller function through EAX register.
Your explanation is nice but do you expect a beginner in c to understand x86 assembly?
Beauty cannot be defined by abscissas and ordinates; neither are circles and ellipses created by their geometrical formulas.
-
i have this code and it works fine.
int fatt(int n)
{
int fattoriale = 1;if(n==0) return fattoriale; else return fattoriale=n\*fatt(n-1);
}
void main(void)
{
printf("%d\n",fatt(10));fflush(stdin); getchar();
}
my question is why it works fine also here ?
int fatt(int n)
{
int fattoriale = 1;if(n==0) return fattoriale; else fattoriale=n\*fatt(n-1);
}
void main(void)
{
printf("%d\n",fatt(10));fflush(stdin); getchar();
}
in this, there aren't the return in the else.... i am using VS2010 and compile in .c . thanks ps: sorry for my english
Without the 'else' the program flow just goes to the end of the func and returns. However there is no return value, so anything calling fatt() and expecting a return value if the 'else' path is hit is going to be dissapointed.
-
Your explanation is nice but do you expect a beginner in c to understand x86 assembly?
Beauty cannot be defined by abscissas and ordinates; neither are circles and ellipses created by their geometrical formulas.
The point is that values are returned in the eax register. The mathematical operation happens to end up with the result in eax AND eax isn't changed by any other operation. Thus it returns the right value. Problem is that something like an inline optimization could change that.
-
i have this code and it works fine.
int fatt(int n)
{
int fattoriale = 1;if(n==0) return fattoriale; else return fattoriale=n\*fatt(n-1);
}
void main(void)
{
printf("%d\n",fatt(10));fflush(stdin); getchar();
}
my question is why it works fine also here ?
int fatt(int n)
{
int fattoriale = 1;if(n==0) return fattoriale; else fattoriale=n\*fatt(n-1);
}
void main(void)
{
printf("%d\n",fatt(10));fflush(stdin); getchar();
}
in this, there aren't the return in the else.... i am using VS2010 and compile in .c . thanks ps: sorry for my english
For me, both in Debug and Release mode, the compiler (VS 2010) issues a warning for the second version, indicating the problem. I have set Warning level to 3, and I've found that the majority of Warnings at that level indicate true errors. My advice to beginners is: 1. Set Warning level to at least 3 (but see link below) 2. Make sure you first understand the true meaning of each Warning and what kind(s) of problem(s) it could cause at runtime. Then fix it.* Here's an interesting article on how to deal with warnings, including those that are off by default[^].
-
In release mode it will create compilation error. It creates compilation error in VS2008. I got compilation error "error C4716: 'fatt' : must return a value". But in debug mode it works as follows. In both case contents of EAX register is used as the output of fatt() function. In both case EAX holds the output value, therefore the two versions works correctly in debug mode. The result of imul is available in eax register and that is the expected result from the fatt function. Following are the disassembly of two versions of fatt() function. First one with return value and next is without return value.
return fattoriale=n\*fatt(n-1);
01182ED2 mov eax,dword ptr [n]
01182ED5 sub eax,1
01182ED8 push eax
01182ED9 call fatt (1181889h)
01182EDE add esp,4
01182EE1 imul eax,dword ptr [n]
01182EE5 mov dword ptr [fattoriale],eax
01182EE8 mov eax,dword ptr [fattoriale]fattoriale=n\*fatt(n-1);
01182F92 mov eax,dword ptr [n]
01182F95 sub eax,1
01182F98 push eax
01182F99 call fatt (11815FAh)
01182F9E add esp,4
01182FA1 imul eax,dword ptr [n]
01182FA5 mov dword ptr [fattoriale],eaxThe multiplication in n*fatt(n-1) is done with imul operation. And its output is available in EAX register. In first version( with return statement), last two instructions perform the following things. Contents of eax is moved to fattoriale and after that the contents of fattoriale is moved to eax( EAX holds the return value, or result of operation). In second version( without return statement) the last mov is not found. But the output of iMul is in EAX register and therefore the output will be available to caller function through EAX register.
Thanks to all! :) I understood!