Recursion:
1. Call yourself;
2. There must be a condition that tends to terminate.
(Recommended tutorial: java course)
The following is a brief introduction to an example of finding factorial:
public class recursion { public static int fac(int n) { if(n == 1){ return 1; //终止条件 } return n * fac(n-1); //调用自身 } public static void main(String[] args) { System.out.println(fac(5)); } } // 运行结果: 120
The recursive process (first gradient )
#Second dimension: Method invocation requires memory to be allocated on the stack
The stack is first in, last out.
First call fac(5), then gradually call fac(4)... until the termination condition.
The process of pushing onto the stack is the process of passing.
As long as the termination condition return is encountered, the function ends and the value of fac(n) is gradually returned.
The process of popping out of the stack is the process of returning.
Related recommendations: Getting Started with Java
The above is the detailed content of What is recursion. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.