C Recursion
\n\n-- Learning is not only about technology, but also about dreams!
\n\n- \n
- Home \n
- HTML \n
- JavaScript \n
- CSS \n
- Vue \n
- React \n
- Python3 \n
- Java \n
- C \n
- C++ \n
- C# \n
- AI \n
- Go \n
- SQL \n
- Linux \n
- VS Code \n
- Bootstrap \n
- Git \n
- Local Bookmarks \n
- \n
- Bootstrap3 \n
- Bootstrap4 \n
- Bootstrap5 \n
- \n
- Machine Learning \n
- PyTorch \n
- TensorFlow \n
- Sklearn \n
- NLP \n
- AI Agent \n
- Ollama \n
- Coding Plan \n
C Tutorial
\n\nC Language TutorialC IntroductionC Environment SetupC VScodeC Program StructureC Basic SyntaxC Data TypesC VariablesC ConstantsC Storage ClassesC OperatorsC Decision MakingC LoopsC FunctionsC Scope RulesC ArraysC enum (Enumeration)C PointersC Function Pointers and CallbacksC StringsC StructuresC UnionsC Bit FieldsC typedefC Input & OutputC File I/OC PreprocessorsC Header FilesC Type CastingC Error HandlingC RecursionC Variable ArgumentsC Memory ManagementC Undefined BehaviorC Command Line ArgumentsC Safe FunctionsC Sorting AlgorithmsC Project StructureC ExamplesC Classic 100 ExamplesC Quiz
\n\nC Standard Library
\n\nC Standard Library - Reference Manual<a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library -
\n\n\n\n\n\nDeep Dive
\nScripts
\nProgramming
\nSoftware
\nWeb Service
\nComputer Science
\nProgramming Languages
\nWeb Design & Development
\nScripting Languages
\nDevelopment Tools
\nWeb Services
\n\nC Recursion
\n\nRecursion refers to the method of using a function itself within its definition.
\n\n\n\n\nFor example:
\n
\nOnce upon a time, there was a mountain, and in the mountain there was a temple. In the temple, there was an old monk telling a story to a little monk! What was the story? "Once upon a time, there was a mountain, and in the mountain there was a temple. In the temple, there was an old monk telling a story to a little monk! What was the story? 'Once upon a time, there was a mountain, and in the mountain there was a temple. In the temple, there was an old monk telling a story to a little monk! What was the story?...'"
The syntax is as follows:
\n\nvoid recursion()\n{\n statements;\n ... ... ...\n recursion(); /* Function calls itself */\n ... ... ...\n}\n\nint main()\n{\n recursion();\n}\n\n\nFlowchart:
\n\nThe C language supports recursion, meaning a function can call itself. However, when using recursion, programmers need to be careful to define a condition for exiting the function; otherwise, it will enter an infinite loop.
\n\nRecursive functions play a crucial role in solving many mathematical problems, such as calculating the factorial of a number, generating the Fibonacci sequence, and so on.
\n\nFactorial of a Number
\n\nThe following example uses a recursive function to calculate the factorial of a given number:
\n\nExample
\n\n#include <stdio.h>\n\ndouble factorial(unsigned int i)\n{\n if(i <= 1)\n {\n return 1;\n }\n return i * factorial(i - 1);\n}\n\nint main()\n{\n int i = 15;\n printf("%d 's factorial is %fn", i, factorial(i));\n return 0;\n}\n\n\nWhen the above code is compiled and executed, it produces the following result:
\n\n15 's factorial is 1307674368000.000000\nFibonacci Sequence
\n\nThe following example uses a recursive function to generate the Fibonacci sequence for a given number:
\n\nExample
\n\n#include <stdio.h>\n\nint fibonaci(int i)\n{\n if(i == 0)\n {\n return 0;\n }\n if(i == 1)\n {\n return 1;\n }\n return fibonaci(i-1) + fibonaci(i-2);\n}\n\nint main()\n{\n int i;\n\n for(i = 0; i < 10; i++)\n {\n printf("%dtn", fibonaci(i));\n }\n return 0;\n}\n\n\nWhen the above code is compiled and executed, it produces the following result:
\n\n0\n1\n1\n2\n3\n5\n8\n13\n21\n34\n\nAI is thinking...
\n\n\n\n\n\n\n\n3 Notes Write a Note
\n\n- \n
- \n
#0 Wind Chaser Willow
\n\n295***031@qq.com
\n\n\n\nRecursion is a concise concept and also a very useful tool. However, using recursion comes at a cost. Compared to direct statements (like while loops), recursive functions consume more runtime and require a large amount of stack space. Each time a recursive function calls itself, it needs to store its state on the stack so that after it finishes calling itself, the program can return to its original state. Poorly designed recursive functions always cause trouble.
\n\nWind Chaser WillowWind Chaser Willow
\n\n295***031@qq.com
\n\nReference Address 9 years ago (2017-05-23)
\n \n - \n
#0 Ocean in the Wind
\n\n\n\nTo use a recursive method to solve a problem, the following three conditions must be met:
\n\n1. The problem to be solved can be transformed into a new problem, and the solution method for this new problem is still the same as the original solution method, only the object being processed increases or decreases regularly.
\n\nExplanation: The problem-solving method is the same, the parameters passed to the function are different each time (increasing or decreasing regularly). If there is no regularity, it cannot be applied to recursive calls.
\n\n2. This transformation process can be applied to solve the problem.
\n\nExplanation: Using other methods is cumbersome or difficult, while using recursion can solve the problem well.
\n\n3. There must be a clear condition to end the recursion.
\n\nExplanation: It must be possible to end the recursive call at an appropriate place. Otherwise, it may lead to a system crash.
\n\nOcean in the WindOcean in the Wind
\n\n973***430@qq.com 9 years ago (2017-07-26)
\n \n - \n
#0 varible
\n\n\n\n1.
\n\nComputer memory is roughly divided into two types: Heap and Stack.
\n\nThe stack is used for functions.
\n\nWhen a computer calls a function, it uses one layer of the stack;
\n\nConversely, when a function ends (returns), that layer of the stack is released, along with everything defined within that layer of the stack (that function).
\n\nThings not on the stack should be in the heap. (This is the purpose of defining global variables versus local variables.)
\n\nIf too many layers of the stack are called (too many functions), the computer will run out of space!
\n\nTherefore, calling a recursive function will push onto the stack layer by layer, causing the computer to run out of space! (This does not mean recursion is not recommended; it's just a warning.)
\n\n2.
\n\nRecursion means recursing (calling layer by layer) and returning (returning layer by layer), which takes a lot of time! It's easy to exceed time limits!
\n\nHowever, I am not saying not to use recursion, but saying that if an iterative algorithm can be used, it's better not to use a recursive algorithm (for the reasons you know).
\n\n3.
\n\nRecursion is an algorithm characterized by a function calling itself.
\n\n4.
\n\nAs we mentioned in (1.), let's talk about it here: The data structure β stack, can be implemented using recursion.
\n\n5.
\n\nC programs written with recursion are generally very concise.
\n\nFor example: Finding factorial
\n\nIterative:
\n\n
\nlong long int fac(int n) {\n if (n < 0) return -1;\n if (n == 0) return 1;\n long long int sum = 1;\n for (int i = 2;i <= n;i ++)\n sum *= i;\n return sum;\n}Recursive:
\n\n
\nlong long int fac(int n) {\n if (n < 0) return -1;\n if (n == 0) return 1;\n return n * fac(n - 1);\n}6.
\n\nSome algorithms, such as search and backtracking algorithms, breadth-first search algorithms, divide and conquer (binary search), all use recursion.
\n\nvariblevarible
\n\n185***8481@QQ.com 6 years ago (2020-02-04)
\n \n
Click me to share notes
\n\n\n\n\n\nWrite a note...
\n\nImage address
\n\nImage description
\n\n\n\nShare notes
\n\n- \n
- Nickname Nickname (required) \n
- Email Email (required) \n
- Reference Address Reference Address \n
- \n
- Python / Data Science\n
- \n
- Python Tutorial \n
\n
YouTip