js function, simple application

Function:

Encapsulate a relatively independent code block with specific functions to form an independent entity, that is, function.

A name (function name) can be called repeatedly in subsequent development.

The function encapsulates a piece of code that can be reused in the future.

Naming of functions

rule

1. It is composed of letters, numbers, underscores and $symbols, and cannot start with a number
           2. It cannot be a keyword or reserved word, such as for,while,this, name
           3. Case sensitive
       standard
           1. The function name must be meaningful
           2. Observe the hump naming method
           3. It is not recommended to use $as the function name

Function declaration

Function function name (){
  // Function body
}

Function expression

var fn = function() {
  // Function body
}

Formal and argument

1. Formal parameter: when declaring a function, in order to make the function more flexible, some values cannot be fixed. For these fixed values, we can set parameters for the function. This parameter has no specific value and only plays a role in occupying position. We usually call it formal parameter, also known as formal parameter.

2. Actual parameters: if the formal parameters are set when the function is declared, the corresponding parameters need to be passed in when the function is called. We call the passed in parameters as actual parameters, also known as actual parameters.

Simply put: Create function gg (n) {}         Call function gg (10)

n above is the formal parameter and 10 is the argument

Return value: used to return the value in the function

1. If the function does not use the return statement, the function has the default return value: undefined

2. If the function uses a return statement, the value following return becomes the return value of the function

3. If the function uses a return statement, but there is no value after return, the return value of the function is: undefined

4. After the function uses the return statement, the function will stop and exit immediately after executing the return statement, that is, all other code after the return will not be executed.

The recommended approach is to either have the function always return a value or never return a value.

(function is also a data type and can be called as a parameter)

Simple case: (comment shortcut: Ctrl + /)

// function getSum(n,m){
//         if(n<=m){
//             var sum = 0;
//           for (;n <= m;n++){
//               sum += n;
//           }
//           console.log(sum);
//         }
//         else{
//             var sum = 0;
//           for (;m <= n;m++){
//               sum += m;
//           }
//           console.log(sum);
//         }
//     }
// N = number (prompt ('Please input the starting number '))
// m=Number(prompt('Please input the ending number '))
// getSum(n,m)
// Area of circle
// function circle(r){
//       var area = Math.PI * r * r;
//       console.log(area);
//     }
// r=Number(prompt('Please enter radius'))
// circle(r)
// Maximum of
function getMaxNum(m,n){
      if (m > n){
        console.log("Maximum:"+ m);
      }else if (m < n) {
        console.log("Maximum:"+ n);
      }else {
        console.log( m +"And"+ n + "equal");
      }
    }
// n=Number(prompt('Please enter a number '))
// m=Number(prompt('Please enter a number '))
// getMaxNum(m,n)

    function getMaxNum(a,b,c){
        if(a>b){
            if(a>c){
                    console.log('The maximum value is:'+a);
            }else if(a==c){
                    console.log('Maximum:'+a);
            }else if(a<c){
                    console.log('The maximum value is:'+c);
            }    
        }else if(a==b){
            if(a>c){
                    console.log('The maximum value is:'+a);
            }else if(a==c){
                    console.log('Three numbers are equal,The value is:'+a);
            }else if(a<c){
                    console.log('The maximum value is:'+c);
            }
        }else if(a<b){
                if(b>c){
                    console.log('The maximum value is:'+b);
                }else if(b==c){
                    console.log('Maximum:'+b);
                }else if(b<c){
                    console.log('The maximum value is:'+c);
                }
        }
    }
// a=Number(prompt('Please enter a number '))
// b=Number(prompt('Please enter a number '))
// c=Number(prompt('Please enter a number '))
// getMaxNum(a,b,c)
// Judge whether a number is prime
function suShu(n){
    var flag=true
    for(var i=2;i<n;i++){
        if(n%i==0){
            flag=false;
            break;
        }
    }
    if(flag){
        alert('It's a prime')
    }else{
        alert('Not prime')
    }
}
// N = number (prompt)
// suShu(n)
// function sss(arguments){
//     var s=0;
//     for(var i=0;i<arguments.length;i++){
//         s+=arguments[i]
//     }
//     console.log(s);
// }
// var a=[1,3,2]
// sss(a)
// Factorial
function getJie(n){
      var jie = 1;
      if(n==0){
        return 1;
      }
      for(var i=1;i<=n;i++){
        jie *= i;
      }
      return jie;
    }
var jiecheng =getJie(7);
console.log(jiecheng);


</script>

Keywords: Javascript Front-end

Added by deepson2 on Mon, 22 Nov 2021 23:47:13 +0200