Pay attention to the details of return usage

Source: Internet
Author: User
Method Declaration: modifier return type method name (parameter ...)
The return type specifies the type of the amount added after return. If the return type is declared as void, the return value is not required.
Public static void main ()// Program The entry can only be declared as void and Int.
{
// OK if no return value is required
}
Public static void mm ()
{
Return; // sometimes, in the Void method, you need to jump out of it, you can directly use return without adding any amount to the back
}
Public static int CC ()
{
Return 321; // an integer is returned because the return type is declared as Int.
}
Public static string msmsm ()
{
Return "asdfhasjghdg"; // a string is returned because the return type is declared as string.
}
-------------------------------------------------------------------------

The return method is often used in functions, and its usage is also classic.

In the following sections, I will pay attention to the usage of return:

Usage 1: return function value:

Example 1: compile a compute class function:

Function JC (n ){

VaR j = 1;

For (VAR I = 0; I <= N; I ++ ){

J * = I;

}

Return J;

}

Alert (JC (5 ));

Calculate 5 here !, The output is 120;

Here, the return function is the value of the return function;

Example 2: Compile another compute class function:

Function JC (n ){
VaR j = 1;
For (VAR I = 0 ;;){
J * = ++ I;
If (I = N) {return J ;}
}

}

Alert (JC (5 ));

Calculate 5 here !, The output is 120;

Here, the return function is to jump out of the loop and jump out of the function. In the return function value;

Example 3: Another return example

Function CS (n ){

If (n = 1) {retrun 1 ;}

If (n = 2) {return ;}

Else {}

}

Note: When the parameter is 1, the function returns 1 and jumps out of the function,

When the parameter is 2, the function return value is null and jumps out of the function,

When the function is another value, continue to execute the following statements of the function until the next return or all statements are executed.

The preceding example shows that return can return the value of a function and jump out of the function;

 

The above example only describes the general purpose of return. Next, I will pay attention to it;

Return can return many data types. So far, I have not found that he cannot return anything.

This includes operations on the Dom, for example, or an example;

Example 4:

<Body>
<Div id = "KK"> AA </div>
</Body>
<SCRIPT>
Function CS (IDS ){
Return document. getelementbyid (IDS). firstchild. nodevalue;
}
Alert (CS ("KK "));

// The value in the DIV is aa;

Alert (document. getelementbyid (kk). firstchildnodevalue );

// Although the returned Div value is also AA;

But they are different. Do you know why ???

For example:

Document. getelementbyid (IDS). firstchild. nodevalue = "BB ";

In this case, the value in the DIV will be changed from AA to BB;

Everyone knows this,

When Cs ("KK") = "BB ";

Would you change the value in the DIV from AA to BB?

Here, you will understand that the objects returned by return cannot be assigned values;

That is to say, CS ("KK") = "BB"; errors will occur when assigning values in this way,

Here is what I want to talk about. Pay attention to the return !!!

More questions to be studied ......

</SCRIPT>

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.