Javascript nested functions - recursive functions - detailed explanation of built-in functions

Javascript nested functions - recursive functions - detailed explanation of built-in functions

Table of contents

Javascript nested functions - recursive functions - detailed explanation of built-in functions

1. Nested functions

2. Recursive function

3. Built-in functions


 

In addition to understanding the definition and function calling of functions, let's introduce several special functions in JavaScript.

There are three types of JavaScript special functions:

  • (1) Nested functions;
  • (2) Recursive function;
  • (3) Built-in functions;

Below I will explain to you in detail the three function calling methods.

 

1. Nested functions

Nested functions, as the name suggests, define another function inside a function. However, functions defined internally can only be called internally. If they are called externally, an error will occur.

Example:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
        //定义阶乘函数
         function fun(a)
         {
             //嵌套函数定义,计算平方值的函数
             function multi (x)
             {
                 return x*x;
             }
             var m=1;
             for(var i=1;i<=multi(a);i++)
             {
                 m=m*i;
             }
             return m;
         }
         var sum =fun(2)+fun(3);
         document.write(sum);
    </script>
</head>
<body>
</body>
</html>

The preview effect in the browser is as follows:

 

5e12a11d7140c036796dbdec6807e33b.png

The multi function defined above can only be used inside the fun function. If it is called outside the fun function, an error will occur. You can modify the code yourself in the "online test" to test it.

 

2. Recursive function

Recursive function is a very important programming technology. I often used it when I was learning other programming technologies (such as C, C++, Java, etc.).

Recursive functions are used to allow a function to call itself from within. However, it should be noted that if the recursive function is not handled properly, the program will fall into an "infinite loop". In order to prevent the occurrence of "infinite loops", you can design a variable that performs self-increment operations to record the number of times the function itself is called. If the number of calls is too many, it will automatically exit the loop.

grammar:

function 递归函数名(参数1)
{
    递归函数名(参数2)
}

illustrate:

When defining a recursive function, 2 necessary conditions are required:

(1) First include a condition to end the recursion;

(2) Secondly, include a recursive call statement;

Example:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
         var msg="\n函数的递归调用:\n\n";
         //响应按钮的点击事件
         function Test()
         {
             var result;
             msg+="调用语句:\n";
             msg+="    result=sum(20);\n";
             msg+="调用步骤:\n";
             result=sum(20);
             msg+="计算结果:\n";
             msg+="    result="+result+"\n";
             alert(msg);
         }
         //计算当前步骤加和值
         function sum(m)
         {
             if(m==0)
             {
                 return 0;
             }
             else
             {
                 msg+="    result="+m+"+sum("+(m-2)+ ");\n";
                 result=m+sum(m-2);
             }
             return result;
         }
    </script>
</head>
<body>
    <input type="button" value="测试" onclick="Test()"/>
</body>
</html>

The preview effect in the browser is as follows:

 

760dd40f1d8b548d142f8dfc1d49085a.png

In the above code, in order to find the sum of even numbers within 20, the recursive function sum(m) is defined, and the function Test() calls it and uses the alert() method to pop up the corresponding prompt information.

Recursive functions may be difficult to understand for beginners. If you really don't know how, you can just ignore it. Because recursive functions are rarely used in JavaScript, recursive functions are often used more in other programming languages. It will be OK if we come back and look through it when needed.

 

3. Built-in functions

There are two types of functions in JavaScript: one is a user-defined function, and the other is a function that has been defined within the JavaScript language and can be called directly by us (that is, a built-in function).

Since built-in functions have been defined within the JavaScript language, we can use them without defining them ourselves. This greatly facilitates our programming.

Regarding JavaScript built-in functions, we will explain them in detail in the next chapter.

 

Guess you like

Origin blog.csdn.net/2301_78835635/article/details/134738335