From the course: C Essential Training

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Using recursion

Using recursion

- [Instructor] In mathematics a recursive function is a function that refers to itself. For example, a factorial indicated mathematics by the exclamation mark, is defined as the product of all positive integers less than or equal to its prime factor. In other words, the factorial of five is equal to five times the factorial of four. This is an example of recursion in mathematics. The factorial of n is equal to n times the factorial of n minus one. Let's try this in C . This is recursive factorial.cpp from chapter six of the exercise files. And here we have a simple function called factorial which takes an unsigned long and returns an unsigned long, and it calls itself. It returns n times the factorial of n minus one, calling its own function. And if n is less than two it returns one and that ends the recursive loop. So the reality here is for every recursive function call memory is allocated for the parameters…

Contents