Home Web Front-end JS Tutorial Recursive function in JS

Recursive function in JS

Feb 08, 2017 pm 03:24 PM
recursive function

In programming languages, when function Func(Type a,...) directly or indirectly calls the function itself, the function is called a recursive function. Recursive functions cannot be defined as inline functions.

Recursive function:

function factorical(num){
  if(num<=1){
    return 1;
  }
  else{
    return num*factorical(num-1);
  }
}
factorial(2)//2
Copy after login

This recursive function uses a function to call the function itself, but is this really good? Next, look here

var another=factorical;
factorical=null;
console.log(another(2))//会报错说 factorical not a function
Copy after login

This is the disadvantage of function calling function, so how to solve it, look below

function factorical(num){
  if(num<=1){
    return 1;
  }
  else{
    return num*arguments.callee(num-1);
  }
}
var another=factorical;
factorical=null;
console.log(another(2))//2
Copy after login

Using arguments.callee instead of the function name above ensures that no error occurs no matter how the function is called.

The above is the recursive function in JS introduced by the editor to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank you all for your support of the PHP Chinese website!

For more articles related to recursive functions in JS, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the excessive function nesting error in Python code? How to solve the excessive function nesting error in Python code? Jun 25, 2023 pm 12:35 PM

Python is a very powerful programming language, and many programmers choose Python as their main programming language. However, too much function nesting in the code can make the program difficult to maintain and understand. This article will explore how to solve the excessive function nesting error in Python code. A brief introduction to function nesting Function nesting refers to the process of defining another function in the body of a function. Function nesting can make the structure of the program clearer and the code easier to read and maintain. However, too many nested functions can lead to an overly complex code structure.

What are the optimization techniques for C++ recursive functions? What are the optimization techniques for C++ recursive functions? Apr 17, 2024 pm 12:24 PM

To optimize the performance of recursive functions, you can use the following techniques: Use tail recursion: Place recursive calls at the end of the function to avoid recursive overhead. Memoization: Store calculated results to avoid repeated calculations. Divide and conquer method: decompose the problem and solve the sub-problems recursively to improve efficiency.

Application of C++ recursive functions in search algorithms? Application of C++ recursive functions in search algorithms? Apr 17, 2024 pm 04:30 PM

Recursive functions are used in search algorithms to explore tree-like data structures. Depth-first search uses a stack to explore nodes, while breadth-first search uses a queue to traverse layer by layer. In practical applications, such as finding files, recursive functions can be used to search for a given file in a specified directory.

What are the exit conditions for a recursive function in C++? What are the exit conditions for a recursive function in C++? Apr 17, 2024 am 11:33 AM

The exit conditions of C++ recursive functions include: Baseline conditions: Check whether the function reaches a state that can directly return results, usually judging whether a certain condition or parameter value meets the threshold. Recursion termination condition: Alternative to or in addition to the baseline condition, ensuring that the function stops after a certain number of recursive calls, by tracking the recursion depth or setting a maximum recursion depth limit.

Application of C++ recursive function in sorting algorithm? Application of C++ recursive function in sorting algorithm? Apr 17, 2024 am 11:06 AM

The application of recursive functions in sorting algorithms in C++ The insertion sort and merge sort algorithms implemented by recursive functions can decompose complex problems into smaller sub-problems and solve them efficiently through recursive calls. Insertion sort: Sorts an array by inserting elements one by one. Merge sort: Divide and conquer, split the array and recursively sort the sub-arrays, and finally merge the sorted sub-arrays.

How to implement factorial using recursive functions in Go language? How to implement factorial using recursive functions in Go language? Jul 31, 2023 pm 08:31 PM

How to implement factorial using recursive functions in Go language? Factorial is a common calculation in mathematics that multiplies a non-negative integer n by all positive integers smaller than it, up to 1. For example, the factorial of 5 can be expressed as 5!, calculated as 54321=120. In computer programming, we often use recursive functions to implement factorial calculations. First, we need to understand the concept of recursive functions. A recursive function refers to the process of calling the function itself within the definition of the function. When solving a problem, a recursive function will continually

Python programming: recursive and anonymous functions and function attributes and documentation strings (function supplement) Python programming: recursive and anonymous functions and function attributes and documentation strings (function supplement) Apr 12, 2023 pm 11:22 PM

This article briefly explains it, supplemented by code to further deepen understanding. Recursive Functions When a function calls itself to produce the final result, such a function is called recursive. Sometimes recursive functions are useful because they make writing code easier - some algorithms are very easy to write using the recursive paradigm, while others are not. There is no recursive function that cannot be rewritten in an iterative way, in other words, all recursive functions can be implemented iteratively through a loop, so it is usually up to the programmer to choose the best approach based on the situation at hand. The body of a recursive function usually has two parts: one part's return value depends on subsequent calls to itself, and the other part's return value does not depend on subsequent calls to itself (called the base case, or recursion boundary). As a reference example for understanding,

How to implement the tail recursion optimization strategy of C++ recursive functions? How to implement the tail recursion optimization strategy of C++ recursive functions? Apr 17, 2024 pm 02:42 PM

The tail recursion optimization strategy effectively reduces the function call stack depth and prevents stack overflow by converting tail recursive calls into loops. Optimization strategies include: Detect tail recursion: Check whether there are tail recursive calls in the function. Convert functions into loops: Use loops instead of tail-recursive calls and maintain a stack to save intermediate state.

See all articles