官术网_书友最值得收藏!

Functions

Now let's cover the very important topic of functions. Functions are simply named blocks of code that execute when called. The vast majority of the code that you write in this course and in your development efforts will occur within functions that you define.

Best practice calls for you to split your code into functions that perform small, discrete tasks. These blocks of code are normally defined in the <head> section of a web page inside a <script> tag, but can also be defined in the <body> section. However, in most cases you will want your functions defined within the <head> section so that you can ensure that they are available once the page has loaded.

To create a function you need to use the function keyword followed by a function name that you define and any variables necessary for the execution of the function passed in as parameter variables. In the event that you need your function to return a value to the calling code, you will need to use the return keyword in conjunction with the data you want passed back.

Functions can also accept parameters which are just variables that are used to pass information into the function. In the following code example, the multiplyValues() function is passed two variables: a and b. This information, in the form of variables, can then be used inside the function which, in this instance, returns the product of a and b, which is assigned to the variable x:

  var x;  
  function multiplyValues(a, b) {
     x = a * b;
return x;
}
主站蜘蛛池模板: 广河县| 普兰店市| 芜湖市| 客服| 高清| 广元市| 汨罗市| 江西省| 抚州市| 左权县| 青神县| 新宁县| 大宁县| 宜良县| 兰溪市| 岳普湖县| 仁布县| 宜丰县| 嘉峪关市| 怀安县| 东源县| 南和县| 南丰县| 大竹县| 靖边县| 自贡市| 图木舒克市| 射洪县| 毕节市| 武功县| 林甸县| 桃源县| 西华县| 利川市| 江北区| 五峰| 柯坪县| 习水县| 东源县| 河津市| 长宁区|