Logo

Programming-Idioms

History of Idiom 31 > diff from v64 to v65

Edit summary for version 65 by zqwnvl:
[Java] Improve formatting

Version 64

2021-08-15, 20:40:29

Version 65

2021-08-16, 03:47:39

Idiom #31 Recursive factorial (simple)

Create recursive function f which returns the factorial of non-negative integer i, calculated from f(i-1)

Idiom #31 Recursive factorial (simple)

Create recursive function f which returns the factorial of non-negative integer i, calculated from f(i-1)

Variables
f,i
Variables
f,i
Code
int f(int i){
	if(i==0)
		return 1;
	else
		return i * f(i-1);
} 
Code
int f(int i) {
    if (i == 0)
        return 1;
    else
        return i * f(i - 1);
}
Comments bubble
Warnings :
- type int quickly overflows
- high number of recursive calls may cause a stack overflow
- also, f is not tail-recursive
Comments bubble
Warnings :
- type int quickly overflows
- high number of recursive calls may cause a stack overflow
- also, f is not tail-recursive
Demo URL
http://ideone.com/G8eCnz
Demo URL
http://ideone.com/G8eCnz