javascript--what is a function

Source: Internet
Author: User

A function is a set of statements that complete a particular function. Without a function, it may take five lines, 10 rows, or even more code to complete the task. In this case, we can put the code block that completes the specific function into a function, call this function directly, save the trouble of repeatedly inputting a lot of code.

How do you define a function? The basic syntax is as follows:

function name () {function code;}

Description

1. function defines the keyword for the functions.

2. "Function name" you take the name of the function.

3. "Function code" is replaced with code that completes a specific function.

Let's write a simple function that implements the addition of two numbers and give the function a meaningful name: "ADD2", the code is as follows:

function Add2 () {   var sum = 3 + 2;   alert (sum);}

Function call:

Once the function is well defined, it cannot be executed automatically, so it needs to be called, just write the function directly where it is needed, and the code is as follows:

javascript--what is a function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.