search

What is recursion

Sep 29, 2020 pm 03:58 PM
recursion

What is recursion

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 )

What is recursion

#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.

What is recursion

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.

What is recursion

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!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.