当前位置:网站首页>Functions (arguments, formal parameters, bubbling)

Functions (arguments, formal parameters, bubbling)

2022-06-12 03:15:00 No two love letters

One 、 Use of functions :

1、 Declare functions

function Function name (){

        // The body of the function

}

2、 Transfer function

Function does not call , Don't do it yourself .

Function name ();

Such as sayHi();

Two 、 Calculation 1-100 Between numbers and


        function getsum() {
            var sum = 0;
            for (var i = 0; i <= 100; i++) {
                sum += i;
            }
            console.log(sum);
        }
        getsum();

3、 ... and 、 Formal parameters and actual parameters

function Function name ( Shape parameter 1, Shape parameter 2.....){

// The body of the function

}

Function name ( Actual parameters 1, Actual parameters 2....);

Inside the parentheses of the declared function are the formal parameters , Inside the parentheses of the function call are the arguments

A formal parameter is a parameter that receives an argument , A formal parameter is similar to a variable

 function cook(aru) {
            console.log(aru);
        }
        cook(' sour and spicy shredded potatoes ');

Four 、 The quantity matching problem of arguments and formal parameters

1、 If the number of arguments is the same as the number of formal parameters , Then the normal output results

2、 If the number of arguments is greater than the number of formal parameters , Then the number of formal parameters will be obtained

3、 If the number of arguments is less than the number of formal parameters , More than formal parameters are defined as underfined, The end result is NaN

5、 ... and 、 The return value of the function

return sentence :

function  Function name (){

        //return The results that need to be returned ;

}

Function name ();

Our function just implements some function , The final result needs to be returned to the caller of the function, the function name () adopt reurun Realized

As long as the function encounters return Just take the results later     Return to the caller of the function

function getResult() {
            return 666;
        }
        getResult(); //getResult()=666
        console.log(getResult());

1、 Find the sum of two numbers

function getSum(num1, num2) {
            return num1 + num2;
        }
        console.log(getSum(1, 2));

2、 Compare the size of two values

function getMax(num1, num2) {
            if (num1 > num2) {
                return num1;
            } else {
                return num2;
            }
        }
        console.log(getMax(1, 3));

3、 Compare the size of two values ( Ternary method )

function getMax(num1, num2) {
            return num1 > num2 ? num1 : num2;
        }
        console.log(getMax(1, 3));

4、 Find the maximum value of an array

 function getArrMax(arr) {
            var max = arr[0];
            for (var i = 1; i <= arr.length; i++) {
                if (arr[i] > max) {
                    max = arr[i];
                }
            }
            return max;
        }
        var re = getArrMax([5, 2, 99, 101, 67, 77]);
        console.log(re);

In actual development , We often use a variable to   Receive the return result of the function

var re = getArrMax([5, 2, 99, 101, 67, 77]);
        console.log(re);

5、 Use the function to find the maximum value of different arrays at the same time

function getMax() {
            var max = arguments[0];
            for (var i = 1; i <= arguments.length; i++) {
                if (arguments[i] > max) {
                    max = arguments[i]
                }
            }
            return max;
        }
        console.log(getMax(1, 2, 3));
        console.log(getMax(1, 2, 3, 4, 5));
        console.log(getMax(1, 2, 34, 444, 5, 100));

6、 ... and 、return

1、return Is the terminating function , The following code will not be executed

2.1、return Only one value can be returned , If you enter multiple values , Subject to the last one

2.2  If you want to use return Return multiple values , You can add an array symbol [  ]

 function getResult(num1, num2) {
            return [num1 + num2, num1 - num2, num1 * num2, num1 / num2];
        }
        console.log(getResult(4, 2));

3 If the function has return, Is returned return Value after ; If the function does not return, Then return to undefined

7、 ... and 、break,continue,return The difference between

break: End the current cycle

continue: Jump out of this cycle , Continue with next cycle

return: You can not only exit the loop , Can also return return Value in statement , It can also end the code in the current function body

8、 ... and 、Arguments function

8.1 It is a built-in object of the current function , It stores all the passed arguments .

8.2 It is a pseudo array , It's not really an array , have length attribute

8.3  Stored by index

8.4  It doesn't have some real array methods :pop(),push() etc.

8.5  You can traverse in an array arguments

8.6  Only functions have argements object

Nine 、 Packaging function

1、 Flip function

 function reverse(arr) {
            var newArr = [];
            for (var i = arr.length - 1; i >= 0; i--) {
                newArr[newArr.length] = arr[i];
            }
            return newArr;
        }
        var arr1 = reverse([1, 3, 4, 6, 9]);
        console.log(arr1);

2、 Bubble function

function sort(arr) {
            for (var i = 0; i < arr.length - 1; i++) {
                for (var j = 0; j < arr.length - i - 1; j++) {
                    if (arr[j] > arr[j + 1]) {
                        var temp = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = temp;
                    }
                }
            }
            return arr;
        }
        var arr1 = sort([1, 4, 2, 9]);
        console.log(arr1);

3、 Judgement of leap year

function isRunYear(year) {
            var flag = flase;
            if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                flag = true;
            }
            return flag;
        }
        console.log(isRunYear(2000));

4、 Function call ( Judgement of leap year 2 Month has 29 God )

function backDay() {
            var year = prompt(' Please enter the year ');
            if (isRunYear(year)) {  // Call a function with parentheses 
                alert(' The current year is a leap year 2 Month has 29 God ');
            } else {
                alert(' The current year is a normal year 2 Month has 28 God ');
            }
            return backDay();

            function isRunYear(year) {
                var flag = flase;
                if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                    flag = true;
                }
                return flag;
            }

Ten 、 Two ways to declare variables

1、 Use function keywords to customize functions ( Name the function )

function fn(){
}

fn();

2、 Function expression

var  Variable name =function(){}

var fun=function(aru){

console.log(' I'm a function expression ')

}

原网站

版权声明
本文为[No two love letters]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203011100482499.html