What is a function
Function (nickname: method) : Blocks of code that implement the same function
2. Classification of functions
- System functions (functions in mathematical objects, such as generating random number function random(), rounding up/down function ceil()/floor(), rounding up function round(), solving power function pow(), etc.). System functions are functions defined by JS, which can be called directly.
- User-defined functions refer to functions that users can declare themselves and implement the expected processing functions.
Three, function declaration, call
Function function name (){function body} or function function name (argument 1, argument 2, argument 3, argument….) {function body}
<script>
// Declare ordinary functions
function sum(){
console.log("I was used.");
}
// Declare a function that takes only one argument
function sum1(a){
console.log(a);
}
// Declare a function with multiple arguments
function sum2(a,b){
console.log(a+b);
}
</script>
Copy the code
Call syntax: function name (); Or function name (parameter 1, parameter 2, parameter 3, parameter….) ;
<script>
// Declare ordinary functions
function sum(){
console.log("I was used.");
}
// Declare a function that takes only one argument
function sum1(a){
console.log(a);
}
// Declare a function with multiple arguments
function sum2(a,b){
console.log(a+b);
}
// Call a normal function
sum();
// Call a function with one argument
sum1(25);
// Call a function with multiple arguments
sum2(25.12);
</script>
Copy the code
After the function is declared, it must be called before it can be executed. If we compare the function to a washing machine, this function can complete the washing function. However, if there is no manual operation, the washing machine cannot be started and the clothes cannot be washed. So once a function is defined, it must be called before it can be executed.
4. Parameters of a function
4.1 classification of function parameters
-
Formal parameters: Arguments written inside the parentheses of the function name when declaring a function
-
Actual arguments: Arguments written inside the parentheses of the function name when a function is called
<script>
// Declare the function
function sum(a,b){ //a,b are the parameters
console.log(a+b);
}
// Call the function
sum(25.30);// 25,30 are arguments
</script>
Copy the code
Parameter passing: Function calls pass values to function parameters via arguments, which then pass values inside the function. The passing direction is the passing of the argument to the parameter. (Note that parameters cannot pass values to arguments).
Functions of function Encapsulation & Function encapsulation
Function encapsulation improves code reuse and program maintainability
- Common implementation functions
- Put it in the shell of function
- Take a name
- Consider parameters – parameters are optional and can be multiple
- Call a function
- Write a function instruction manual (comment)
- Function description
- Description of function parameters
The return value of the function
After a function is wrapped, the result of processing can only be used inside the function without any operation, that is, the result of the function processing is a local variable without any operation. So how do you get the result of a function processing outside of a function?
- You can assign the result of the function processing to the global variable by using the global variable, and then use the (Disadvantages, multiple functions, it is necessary to declare multiple global variables, easy to cause memory waste; If there is only one or fewer global variables, variable pollution may occur.
- through
return
Throwing the value (back to where the function was called) benefits: No memory waste.
6.1 Two Uses of Return
- Returns the result of the function processing
- Terminating function (similar to break. Break can only be used in a statement, return can only be used in a function.
Return Program data flow
Return after console.log()