JavaScript keyword return usage

Source: Internet
Author: User

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

Syntax:

Return [() [expression] [];

The optional expression parameter is the value to be returned from the function. If omitted, the function does not return a value.

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

The following example illustrates the usage of the return Statement:

Function myfunction (arg1, arg2) {var r; r = arg1 * arg2; return (r );}

Return: return from the called function to the main function for execution. A return value can be included in the return, which is specified by the parameters following return. Return is usually necessary, because the computation results are usually obtained through the return value during function calls.

If you do not need the function to return any value, you need to use void to declare its type.

Supplement: If you have a return type definition before the function name, such as int or double, you must have a return value. If it is void, you can leave the return value blank, however, even if the data is written, the following values cannot be returned:

The following is a non-void function:

Int f1 () {int I = 1; return 1; // return (I); // This can also be done}

Void functions:

Void f2 () {int I = 1; // return; // This can also be done. Do not use this sentence}

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.