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

Functions

Functions are very important when working with JavaScript. We will also use functions a lot in our examples.

The following code demonstrates the basic syntax of a function. It does not have arguments or the return statement:

function sayHello() { 
  console.log('Hello!'); 
} 

To call this code, we simply use the following call:

sayHello(); 

We can also pass arguments to a function. Arguments are variables with which a function is supposed to do something. The following code demonstrates how to use arguments with functions:

function output(text) { 
  console.log(text); 
} 

To use this function, we can use the following code:

output('Hello!'); 

You can use as many arguments as you like, as follows:

output('Hello!', 'Other text'); 

In this case, only the first argument is used by the function and the second one is ignored.

A function can also return a value, as follows:

function sum(num1, num2) { 
  return num1 + num2; 
} 

This function calculates the sum of two given numbers and returns its result. We can use it as follows:

var result = sum(1,2); 
output(result); 
主站蜘蛛池模板: 东丽区| 崇礼县| 永胜县| 松滋市| 荣成市| 睢宁县| 夏河县| 姜堰市| 策勒县| 岢岚县| 新平| 珲春市| 永寿县| 宁海县| 无极县| 昂仁县| 克什克腾旗| 武鸣县| 汾阳市| 江川县| 福鼎市| 平顶山市| 竹溪县| 五家渠市| 两当县| 清新县| 勃利县| 怀仁县| 深州市| 朔州市| 类乌齐县| 金平| 瑞丽市| 金寨县| 澎湖县| 英德市| 长治县| 彭山县| 合肥市| 广宁县| 柞水县|