JS4 functions (Method function)

Source: Internet
Author: User

What is a function

function, you can write the code once, and then reuse the code over and over again.

Defining functions

function function name ()
{
function body;
}

The function defines the key word for functions, "function name" you take for the function name, "function body" is replaced by the code that completes the specific function.

We complete the function of summing the two numbers and displaying the results. and give the function a meaningful name: "ADD2", the code is as follows:

<script type= "Text/javascript" >
function Add2 () {
sum = 3 + 2;
alert (sum);
}
? ADD2 ();</script>

Function call

Once a function is defined, it cannot be executed automatically, it needs to be called, and the function name is written directly where it is needed.

First case: Called within the <script> tag.

<script type= "Text/javascript" >
function Add2 ()
{
sum = 1 + 1;
alert (sum);
}
ADD2 ();//Call function, write function name directly. </SCRIPT>

The second case is called in an HTML file, such as calling a defined function by clicking the button.



<script type= "Text/javascript" >
function Add2 ()
{
sum = 5 + 6;
alert (sum);
}
</script>

<body>
<form>
<input type= "button" value= "click it" onclick= "add2 ()" >//button, onclick Click event, direct write function name
</form>
</body>

function with parameters

The ADD2 () function in the previous section does not implement any of the specified two number additions. In fact, the definition function can also be in the following format:

function function number name (parameter 1, parameter 2)
{
function code
}

Note: The parameters can be more than one, increase or decrease the number of parameters as required. The parameters are separated by (comma,).

In this format, the function implements any two numbers and should be written as:

function Add2 (x, y)
{
sum = x + y;
document.write (sum);
}

X and Y are the two parameters of the function, and when we call the function, we can pass the two actual addend to the function by these two parameters.

For example, ADD2 (3,4) will seek 3+4 and ADD2 (60,20) will find the and of 60 and 20.

function that returns a value

thinking: in the previous section of the function, the result is output by "document.write", what if you want to process the result of the function?

We just change the "document.write" line to the following code:

function Add2 (x, y)
{
sum = x + y;
return sum; Returns the value of the function, which is called the return value.
}

You can also call the function's return value through the variable store code as follows:

result = ADD2 (3,4);//After the statement executes, the value in the result variable is 7.

Note: parameters and return values in a function are not just numbers, they can also be other types such as strings.

JS4 functions (Method function)

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.