课程: Complete Guide to C Programming Foundations

今天就学习课程吧!

今天就开通帐号,24,100 门业界名师课程任您挑!

Creating recursive functions

Creating recursive functions

- [Instructor] A recursive function is one that calls itself. It's like a picture within the same picture or a door that opens into a room with a door that opens into the same room. Yes, this concept is mind-boggling, but recursion has useful purposes. In this code, function recurse calls itself. The problem with recursion, however, is that the function keeps calling itself with no end possible. This condition is a serious concern with recursion. Like a loop, you need a terminating condition for the recursive function. Otherwise, all sorts of bad things can happen, primarily, (laughing) the program crashes. Let's run to find out. Oops. In this configuration, the function has called itself recursively 261,939 times, and a really bad error has occurred. This code is not a successful example of recursion. But it can be fixed. A successful recursion function has an exit condition, where the function returns to itself. (computer keys clicking) The if statement provides the exit condition…

内容