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; }
- Vue.js 3.x快速入門
- Python程序設計教程(第2版)
- 架構不再難(全5冊)
- AWS Serverless架構:使用AWS從傳統部署方式向Serverless架構遷移
- 數據結構(Python語言描述)(第2版)
- 名師講壇:Java微服務架構實戰(SpringBoot+SpringCloud+Docker+RabbitMQ)
- 基于Swift語言的iOS App 商業實戰教程
- C語言程序設計
- jQuery Mobile移動應用開發實戰(第3版)
- 零基礎趣學C語言
- Getting Started with Nano Server
- IBM Cognos TM1 Developer's Certification guide
- NGUI for Unity
- Java EE 8 and Angular
- Neo4j 3.x入門經典