From the course: C Essential Training
Unlock the full course today
Join today to access over 22,400 courses taught by industry experts or purchase this course individually.
Creating recursive functions - C Tutorial
From the course: C Essential Training
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 it has useful purposes. This code shows how it recursive function works though it's imperfect. The recurse function is first called in the main function, it has an argument of zero. Within the recurse function, the recurse function is called again, this time with variable a, which was passed and incremented. The value of variable a increases each time the recurse function calls itself. Calling a same function within itself is the basis of recursion, build and run the code. And on this system, the recurse function calls itself 43,178 times before the stack implodes and the program crashes, this isn't the outcome you want. To write a successful recursive function you must supply an exit condition…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
Understanding functions3m 21s
-
Creating a function3m 28s
-
Challenge: Writing a function46s
-
Solution: Writing a function1m 10s
-
Returning a value from a function2m 33s
-
Challenge: Returning a value42s
-
Solution: Returning a value1m 40s
-
Passing arguments to a function1m 52s
-
Challenge: Passing values44s
-
Solution: Passing values1m 22s
-
Using the main() function's arguments3m 30s
-
Retaining values in a function2m 58s
-
Creating recursive functions3m 53s
-
Challenge: Complete the code1m 14s
-
Solution: Complete the code1m 31s
-
-