Basic Javascript tutorials-definitions and calls of functions-Basic javascript tutorials

Source: Internet
Author: User

Basic Javascript tutorials-definitions and calls of functions-Basic javascript tutorials

A function is a statement that can run at any time. Simply put, a function is a set of statements that complete a function. It accepts 0 or multiple parameters.

Basic Function syntaxAs follows:

Copy codeThe Code is as follows:
Function functionName ([arg0, arg1,... argN]) {
Statement
[Return [expression]
}

Function is the keyword of the custom function, functionName is the function name, arg indicates the list of parameters passed to the function, and each parameter is separated by a comma. The parameter can be null.

Statement is a function of this province. It can be a variety of valid code blocks.

Reture expression is the return function value expression, which is also optional. A simple example is as follows.

Copy codeThe Code is as follows:
Function sayName (yname ){
Document. write ("hello" + yname)
}
SayName (1, 112 );

In addition, javascript will not be executed after rerurn is executed.

Copy codeThe Code is 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>

A function may have multiple return

Copy codeThe Code is 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>

The above is all the content of this article. Do you have a new understanding of defining and calling javascript? I hope this article will help you.

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.