JavaScript basic tutorial Definition and call Function _ basics

Source: Internet
Author: User

A function is a statement that can be run at any time, simply put, a function is a set of statements that completes a function, and it accepts 0 or more parameters.

the basic syntax for a function is as follows

Copy Code code as follows:

function functionname ([arg0,arg1,...... argn]) {
Statement
[Return[expression]]
}

Where the function is the keyword of the custom function, functionname is the name of the functions, and Arg represents a list of various parameters passed to the function, separated by commas. The argument can be empty.

Statement is a function of the province, can be a variety of legitimate blocks of code.

Reture expression is also optional for the return function's value expression. A simple example is shown below.

Copy Code code as follows:

function Sayname (yname) {
document.write ("Hello" + yname)
}
Sayname (112);

In addition, JavaScript does not execute Rerurn after it has been executed.

Copy Code code as follows:

<div id= "xxx" style= "Width:200px;height:100px;background-color:aquamarine" ></div>
<script type= "Text/javascript" >
function Cnumber (InNmuber1, InNumber2) {
return InNmuber1 + inNumber2
}
Irese = Cnumber (40,20);
document.getElementById ("xxx"). InnerHTML = Irese;
</script>

There may be more than one return in a function

Copy Code code as follows:

<div id= "xxx" style= "Width:200px;height:100px;background-color:aquamarine" ></div>

<script type= "Text/javascript" >
function Cnumber (InNmuber1, InNumber2) {
if (InNmuber1 >= inNumber2)
Return Innmuber1-innumber2
Else
Return Innumber2-innmuber1
}
Irese = Cnumber (10,20);
document.getElementById ("xxx"). InnerHTML = Irese;
</script>

This is the entire content of this article, the small partners whether the definition and invoke JavaScript have a new understanding of it, I hope this article can help

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.