Function block section automates certain functions, in function, not only addresses the need to reuse a function of the weight, and also avoid bloated code.
Use function has two requirements: You must call before you can perform; function names and keywords, and do not function the same system;
There are three main functions, direct calls, parameter passing, return value.
Direct call:
. 1 <! DOCTYPE HTML > 2 < HTML lang = "EN" > . 3 < head > . 4 < Meta charset = "UTF-. 8" > . 5 < title > area of the circle </ title > . 6 < Script type = "text / JavaScript " > 7 // find any radius of a circle, with an area . 8 function mianji () { . 9 var R & lt = prompt ( " Please enter the radius of the circle: " ); 10 S = 3.14 * R & lt * R & lt; . 11 Alert ( " area of the circle is " + S); 12 is } 13 is mianji (); 14 </ Script > 15 </ head > 16 < body > . 17 18 is </ body > . 19 < / HTML >
Parameter passing:
. 1 <! DOCTYPE HTML > 2 < HTML lang = "EN" > . 3 < head > . 4 < Meta charset = "UTF-. 8" > . 5 < title > area of the circle </ title > . 6 < Script type = "text / JavaScript " > 7 // find any radius of a circle, with an area . 8 function miaji (R & lt) { . 9 S = 3.14 * R & lt * R & lt; 10 alert(" Circle area: " + S); . 11 } 12 is miaji ( 2 ); 13 is miaji ( . 3 ); 14 15 </ Script > 16 </ head > . 17 < body > 18 is . 19 </ body > 20 is </ HTML >
However, in general the development of the front end, the third is used, the return value is.
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>函数</title> 6 <script type="text/javascript"> 7 //返回值 8 function getSum(n,m){ 9 var sum=0; =inwas(for10 n-; I <= m; I ++ ) { . 11 SUM + = I; 12 is } 13 is // returns the calculated value 14 return SUM; 15 } 16 the console.log (GetSum ( . 1 , 100 )); . 17 </ Script > 18 is </ head > . 19 < body > 20 is </ body > 21 is </ HTML >