Is a recursive function a function that calls itself and loops?

Asked 1 years ago, Updated 1 years ago, 44 views

Is a recursive function a function that calls itself and loops?

Example

function myChange(){
    setTimeout (myChange, 5000); 
    letterative=()=>{
    interactive = setTimeout (myChange, 5000); 
}

________

Thank you.

It's difficult for beginners to understand, but
Call stack is to temporarily save data in progress to some areas of main memory when you look at the information below.
Can I think of it as ?

>>>
Stack [2][stack]
Temporarily save data being processed by a computer.Also, the storage area and the data structure for that purpose. Data stored later are taken out first. → Post-entry first-out method

A call stack is a stack that stores information about subroutines during execution in a program.
Running subroutines mean subroutines that have been called but have not been processed.

Do you mean that my source is looping the defined self, but it doesn't happen, so I can't call it a recursive function?
Then how can I determine if a call stack occurs or does not occur?

javascript

2022-09-30 21:24

3 Answers

Depending on the definition of the word recursive function, for example, C is also mentioned in the language specification.

6.5.2.2 Function calls

11.Recursive function calls shall be permitted, both directly and indirectly through any chain of other functions.

(International Organization for Standardization, ISO 9899:1999 Programming languages --C, page.72)

4.10 Recursion

C functions may be used recovery; that is, a function may call itself either directly or directly.

(Brian W. Kernighan and Dennis M. Ritchie, The C Programming Language, Second Edition, Patent Hall, 1988, ISBN 0-13-110362-8, 0-13-110370-9, page.86)

14.1.21 Runtime Semantics:Evaluation

Note2 The BindingIdentifier in a FunctionExpression can be referenced from inside the FunctionExpression's FunctionBody to allow the function to call itself recurring.

(Ecma International,ECMA-262ECMAScript®2016 Language Specification, Edition 7, page.283)

In relation to the story in the comment, C says both directly and indirectly.However, JavaScript (ECMAScript) does not mention anything other than itself.

However, it is safe to consider the so-called "mutual recursive function" as a kind of recursive function.They all "ride the call stack multiple times when you call them", so the basis is to behave differently from normal functions.

For your information:

Indirect recovery

Most basic examples of recovery, and most of the examples presented here, demonstrate direct recovery, in which a function calls itself.Indirect recovery occurrences when a function is broken by something. ...
Indirect recovery is also called mutual recovery, which is a more symmetric term, through this is simple a difference of emphasis, not a difference.


2022-09-30 21:24

Now that the question has been added, I would like to mainly try to explain that part.

Consider the behavior of a language that defines a function, such as JavaScript, and allows you to call that function.Even though there is only one function definition, you can call it from different locations in the program.

function myFunc(){
    //...
}

//...

myFunc();//(A)

//...

myFunc();//(B)

When you run a function call called myFunc();, the control moves to the contents of the myFunc() function to execute what is defined, but after you have finished executing the contents of the function, you must continue calling the function.Therefore, when calling a function, you must at least remember somewhere where to go back when you have finished executing the contents of this function.

In a language that allows recursive calls in a common sense, you store this information in a data structure called "stack" (also called FILO).

|
+ - Return after (A) - Call > myFunc() (A)
                     |
                     |
<----------------------------------------------------------------------------------------------------------------
|
(A)continue running after
|
+ - Return after (B) - Call > myFunc() (B)
                     |
                     |
<----------------------------------------------------------------------------------------------------------------
|
(B)continue running after
|
:

By using the call stack in this way, you can continue (A) after the myFunc(); call (A) and (B) after the myFunc(); call (B).

If a function call is embedded in an expression, it will be difficult to explain where "after (x)" is, so I'll skip the details, but think that something is stored in the stack that represents "where formula calculations can continue."

In the example of Spitson's introduced Fibonacci count calculation in a recursive call, the call stack is piled up and pulled out many times before the calculation is completed like this.

|
+--- > Invoke first fib(n)
    |
    +--- Invoking fib(n-1) while running > fib(n)
        |
        +--- Invoking fib(n-2) while running > fib(n-1)
            :
            :
        <------
        |
        +--- Invoking fib(n-3) while running > fib(n-1)
            :
            :
        <------
        |
    <------
    |
    +--- Invoking fib(n-2) while running > fib(n)
        :
        :
    <------
    |
<------
|

When n is a large number, it's a very large figure, so it's quite broken and I'm ignoring the detailed order of execution, but do you understand the atmosphere?

Therefore, this question

How can I determine if a call stack occurs or not?

The answer to is

The call stack is loaded with data when the function is actually called

Data is dropped from the call stack when the function is finished running

In other words, whenever a actual function call occurs, the call stack operation always occurs.

The code you indicated (setTimeout(myChange, 5000); only focuses on) simply instructs JavaScript to "Run this function (myChange) later" and no actual invocation of the function occurs.

Therefore, when myChange() invocation data is loaded into the call stack (that is, the function has not finished running), the call stack does not stack up (multiple, or subsequent) data for invocation.These states are not called recursive calls (and therefore functions defined in such a way are not called recursive functions).

If you can make a good distinction between passing a function as a parameter in the sense of "Run it later" and saying it's different from what it actually does, you'll understand.


2022-09-30 21:24

Yes.
It's like calling yourself.

 function fib(n){
    if(n===0)return0;
    if(n<3)return1;
    return fib(n-2)+fib(n-1);
}

console.log(fib(10));


2022-09-30 21:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.