[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Difference between for and while loop



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

> > Newbish question, what's recursion ?
>
> Simple explanation: function calling itself.
>
> Longer explanation is at
> http://www-2.cs.cmu.edu/~cburch/survey/recurse/recursion.html
Simple example:

(calculation factorials is the most common example of a recursive function)

5 ! = 5 * 4! = 5 * 4 * 3! = 5 * 4 * 3 * 2! = 5 * 4 * 3 * 2 * 1! 
= 5 * 4 * 3 * 2 * 1 

int a = 5;
int b = factor(a);

int factor(int a){
	if(a>1){
	   //	function call to itself
	    return (a *= factor(a-1));
	}else{
	   return 1;
        }
}

- -- 
Homepage: http://www.de-brauwer.be
BSD is for Unix lover
Linux is for windows haters,
I run Linux 
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQE+oDCopSQix3Sc+ZgRAuc8AJ9FSHfMUqWxaOZuAEVlIH9g8jzsAQCfQoeC
G410XAFMS8WFJ9ocrdIOKBs=
=NKs8
-----END PGP SIGNATURE-----



Reply to: