Use of the JavaScript keyword return

Source: Internet
Author: User

The return statement exits from the current function and returns a value from that function.

Grammar:

1 return[()[expression][]];

The optional expression argument is the value to be returned from the function. If omitted, the function does not return a value. Xiping County Quine Friends Electric

Use the return statement to terminate the execution of a function and return the value of expression. If expression is omitted, or if no return statement is executed within the function, the value undefined is assigned to the expression that invokes the current function.

The following example illustrates the use of the return statement:

1 functionmyfunction(arg1, arg2){  
2     varr;  
3     r = arg1 * arg2;  
4     return(r);
5 }

Return indicates that the function returned from the callback to the key function continues execution, which can be returned with a return value specified by the argument following return. Return is usually necessary because the result of a function call is usually taken out of the return value.

If you really do not need a function to return what value, you need to declare its type with void.

Add: If you have a return type definition in front of a function name, such as int,double, you must have a return value, and if it is void, you may not write return, but you cannot return a value even if it is written:

The following is a non-void function:

1 int f1()
2 {
3     int i=1;
4     return1;
5     //return(i);    //这样也可以
6 }

function of void type:

View Source print?
1 void f2()
2 {
3     int i=1;
4     //return;//这样也可以,不要这一句也可以
5 }

Use of the JavaScript keyword return

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.