1. Ordinary function
Grammar:
Function function name (){
Statement block
}
2. Functions with parameters
Grammar:
Function function name (parameter list){
Statement block
}
3. Function with return value
Grammar:
Function function name (parameter list){
Statement block;
Return value;
}
Allow a variable to accept the return value after calling the function
Var variable name = function name (argument list)
practice:
Define a method that can accept three number parameters, in which the maximum value is calculated and returned
function getMax(a,b,c){
var max = a;
b > max && (max = b);
c > max && (max = c);
return max;
}
Maximum calculation
4. Scope
1. Definition: the accessible range of variables and functions, there are mainly two types
(1) Function scope
Access to variables and functions is only allowed within defined functions
(2) Global scope
Once defined, it can be accessed anywhere
2. Variables in function scope
A variable declared in a function is a variable in the scope of the function. It can also be called a local variable
3. Variables in global scope
Once declared, it can be used anywhere, also known as “global variable”
Declaration method:
(1) Declare variables in the outermost layer, except for all functions
(2) When you declare a variable, you do not use the VaR keyword. All variables are global variables, but there is a risk (when you define a function that does not use the VaR keyword to declare a global variable, you can only declare it successfully after the function has run once.)
4. Advance declaration
JS program will read all variables declared by VaR and functions declared by function to the top of the scope before formal execution, but the assignment will remain in the original position
function show(){
console.log (Num); // print undefined
var num = 10;
console.log(num);
}
function show(){
console.log (Num); // error
}
5. Value transfer
function change(a,b){
a ^= b;
b ^= a;
a ^= b;
}
function testchange(){
var a = 15;
var b = 18;
console.log ("before calling):";
console.log(a,b);
change(a,b);
console.log (after call:);
console.log(a,b);
}
Running results
The change() function passes the equivalent copy, so it does not affect the contents of the testchange() function
Suggestion: the data of basic data type should not be modified in the function when passing parameters, because even if it is modified, it will not affect the original data
5. Local function
Declare a function within a function
6. Global function
- paseInt()
- parseFloat()
- Number()
- isNaN()
- Encodeuri (): encodes the string in uniform resource identifier format and returns the encoded string. Encodeuri () compiles multi byte characters into multiple single byte characters.
- Decodeuri (): decodes the encoded URI
- Encodeuricom ponent() allows special symbols (:, /,) to encode on the basis of encodeuri
- decodeURIComponent()
- Eval () computes and executes JS code as a string
7. Recursive call
1. Recursion refers to a function that calls itself once
2. Implementation of recursion
(1) Boundary conditions
(2) Recursion forward – continue to tune yourself
(3) Recursive return – returns data up
3. Problem solving
Calculate the factorial of a number
//Find the factorial function f (n) {// judge whether the boundary condition is reached. If it is, return 1 if (n = = 1) {return 1;} else {return n * f (n-1)}} // modify function f (n) {return n = = 1? 1: n * f (n-1);} through conditional operator
practice:
The following sequence (Fibonacci sequence) is known
1,1,2,3,5,8,13,21,34,55,… …
It is known that the first number is 1 and the second number is 1
Starting from the third number, each number is equal to the sum of the first two numbers
/**
*Finding the value of the nth digit in Fibonacci sequence
*Boundary condition: n = = 1 | n = = 2
*/
function f(n){
if(n == 1 || n ==2){
return 1;
}else{
return f(n-1) + f(n-2)
}
}
console.log(f(5));