[JavaScript] JavaScript learning 12 JavaScript Functions

Source: Internet
Author: User
Document directory
  • Syntax for creating a function:
  • Example

A function is an event-driven or reusable code block that is executed when it is called.

Instance
Function
How to call a function.
Functions with Parameters
How to pass a variable to a function and how to use the variable in the function.
Function 2 with Parameters
How to pass multiple variables to the function, and how to use these variables in the function.
Return Value Function
How to return values from a function.
Functions with parameters and returned values
How to input two parameter values to the function and return values.
JavaScript Functions

Compile the script as a function to avoid executing the script when loading the page.

A function contains code that can only be executed when an event is activated or called.

You can call the script anywhere on the page (if the function is embedded with an external. js file, you can even call it from other pages ).

The function is defined at the beginning of the page, that is, the

<script type="text/javascript">function displaymessage(){alert("Hello World!")}</script>

Suppose alert ("Hello world !! ") Is not written into the function, it will be executed when the page is loaded. The script is executed only when the user hits the button. The onclick event is added to the button, so that the function is executed only when the button is clicked.

In this chapter, you will learn more about JavaScript events.

How to define the syntax for creating a function:
Function Name (var1, var2,..., varx) {code ...}

Var1, var2, and so on refer to the variables or values of the input function. {And} define the start and end of the function.

Note: A function without parameters must be enclosed by brackets after its function name:

Function Name () {code ...}

Note: Do not forget the importance of uppercase/lowercase letters in JavaScript. The word "function" must be in lower case, otherwise JavaScript will fail. In addition, you must use the same case name to call a function.

Return Statement

The return statement is used to specify the value returned from the function.

Therefore, the function that needs to return a value must use this return statement.

Example

The following function returns the values (a and B) multiplied by two numbers ):

function prod(a,b){x=a*breturn x}

When you call the above function, you must input two parameters:

product=prod(2,3)

The return value from the prod () function is 6, which is stored in the variable named product.

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.