YouTip LogoYouTip

C Recursion

C Recursion

\n\n

-- Learning is not only about technology, but also about dreams!

\n\n\n\n\n\n\n\n\n\n\n\n

C Tutorial

\n\n

C 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\n

C Standard Library

\n\n

C 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 - ">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 - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library -

\n\n

C Error Handling

\n\n

C Variable Arguments

\n\n

Deep Dive

\n

Scripts

\n

Programming

\n

Software

\n

Web Service

\n

Computer Science

\n

Programming Languages

\n

Web Design & Development

\n

Scripting Languages

\n

Development Tools

\n

Web Services

\n\n

C Recursion

\n\n

Recursion refers to the method of using a function itself within its definition.

\n\n
\n

For example:
\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?...'"

\n
\n\n

The syntax is as follows:

\n\n
void recursion()\n{\n   statements;\n   ... ... ...\n   recursion(); /* Function calls itself */\n   ... ... ...\n}\n\nint main()\n{\n   recursion();\n}\n
\n\n

Flowchart:

\n\n

Image 2

\n\n

The 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\n

Recursive 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\n

Factorial of a Number

\n\n

The following example uses a recursive function to calculate the factorial of a given number:

\n\n

Example

\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\n

When the above code is compiled and executed, it produces the following result:

\n\n
15 's factorial is 1307674368000.000000
\n

Fibonacci Sequence

\n\n

The following example uses a recursive function to generate the Fibonacci sequence for a given number:

\n\n

Example

\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\n

When the above code is compiled and executed, it produces the following result:

\n\n
0\n1\n1\n2\n3\n5\n8\n13\n21\n34
\n\n

AI is thinking...

\n\n

C Error Handling

\n\n

C Variable Arguments

\n\n

iFlytek StarNet Coding Plan includes free model call quotas for DeepSeek, GLM, Kimi, MiniMax, a one-stop experience and deployment platform. Configuration Guide Β₯3.9/month Activate Now

\n\n

3 Notes Write a Note

\n\n
    \n
  1. \n

    #0 Wind Chaser Willow

    \n\n

    295***031@qq.com

    \n\n

    Reference Address 187

    \n\n

    Recursion 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\n

    Wind Chaser WillowWind Chaser Willow

    \n\n

    295***031@qq.com

    \n\n

    Reference Address 9 years ago (2017-05-23)

    \n
  2. \n
  3. \n

    #0 Ocean in the Wind

    \n\n

    973***430@qq.com 109

    \n\n

    To use a recursive method to solve a problem, the following three conditions must be met:

    \n\n

    1. 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\n

    Explanation: 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\n

    2. This transformation process can be applied to solve the problem.

    \n\n

    Explanation: Using other methods is cumbersome or difficult, while using recursion can solve the problem well.

    \n\n

    3. There must be a clear condition to end the recursion.

    \n\n

    Explanation: It must be possible to end the recursive call at an appropriate place. Otherwise, it may lead to a system crash.

    \n\n

    Ocean in the WindOcean in the Wind

    \n\n

    973***430@qq.com 9 years ago (2017-07-26)

    \n
  4. \n
  5. \n

    #0 varible

    \n\n

    185***8481@QQ.com 132

    \n\n

    1.

    \n\n

    Computer memory is roughly divided into two types: Heap and Stack.

    \n\n

    The stack is used for functions.

    \n\n

    When a computer calls a function, it uses one layer of the stack;

    \n\n

    Conversely, 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\n

    Things not on the stack should be in the heap. (This is the purpose of defining global variables versus local variables.)

    \n\n

    If too many layers of the stack are called (too many functions), the computer will run out of space!

    \n\n

    Therefore, 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\n

    2.

    \n\n

    Recursion 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\n

    However, 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\n

    3.

    \n\n

    Recursion is an algorithm characterized by a function calling itself.

    \n\n

    4.

    \n\n

    As we mentioned in (1.), let's talk about it here: The data structure β€” stack, can be implemented using recursion.

    \n\n

    5.

    \n\n

    C programs written with recursion are generally very concise.

    \n\n

    For example: Finding factorial

    \n\n

    Iterative:

    \n\n
    long 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}
    \n

    Recursive:

    \n\n
    long long int fac(int n) {\n    if (n < 0) return -1;\n    if (n == 0) return 1;\n    return n * fac(n - 1);\n}
    \n

    6.

    \n\n

    Some algorithms, such as search and backtracking algorithms, breadth-first search algorithms, divide and conquer (binary search), all use recursion.

    \n\n

    variblevarible

    \n\n

    185***8481@QQ.com 6 years ago (2020-02-04)

    \n
  6. \n
\n\n

Click me to share notes

\n\n

Cancel

\n\n
    \n
  • \n
  • \n
  • \n
  • \n
  • \n
\n\n

Write a note...

\n\n

Image address

\n\n

Image description

\n\n

Image size Γ—

\n\n

Share notes

\n\n
    \n
  • Nickname Nickname (required)
  • \n
  • Email Email (required)
  • \n
  • Reference Address Reference Address
  • \n
\n\n

Category Navigation

\n\n
← Php Spaceship OperatorPhp Coalescing Operator β†’