JavaScript Intensive Tutorial-what is a function?

Source: Internet
Author: User

This article is the official HTML5 training course of h5edu agency, mainly introduces Javacript intensive tutorial--what is function?

A function is a set of statements that can be run anywhere.

function is the core of ECMAScript.

Functions are declared in such a way that the keyword function, the function name, a set of parameters, and the pending code that is placed in parentheses.

The basic syntax for a function is this:

function functionname (arg0, arg1, ... argN) {  statements}

How do I call a function?

The function can be called by its name plus the arguments in parentheses, if there are multiple arguments.

If you want to invoke the function in the example above, you can use the following code:

Sayhi ("David", "Nice to meet you!")

Calling the above function Sayhi () generates a warning window
How does a function return a value?

The function Sayhi () does not return a value, but it does not have to be specifically declared (as with void in Java).

Even if a function does have a value, it does not have to be explicitly declared. The function only needs to use the return operator followed by the value to be returned.

function sum (INUM1, iNum2) {  return iNum1 + iNum2;}


The following code assigns the value returned by the SUM function to a variable:

var = sum (iresult); alert (iresult);//Output "2"

Another important concept is that, as in Java, a function stops code immediately after executing a return statement. Therefore, the code after the return statement is not executed.

For example, in the following code, the alert window is not displayed:

function sum (INUM1, iNum2) {  return iNum1 + iNum2;  Alert (iNum1 + iNum2);}

There can be multiple return statements in a function, as follows:

function diff (INum1, iNum2) {  if (INum1 > iNum2) {    return inum1-inum2;  } else {    return inum2-inum1;  }}

The function above is used to return the difference of two numbers. To achieve this, the smaller number must be subtracted from the larger number, so the IF statement determines which return statement to execute.

If the function has no return value, you can call the return operator without arguments and exit the function at any time.

For example:

function Sayhi (smessage) {if (smessage = = "Bye") {return;} alert (smessage);}

In this code, if Smessage equals "Bye", the warning box is never displayed.

Note: If the function has no explicit return value, or if a return statement with no arguments is called, then the value it really returns is undefined.

Click to enter JS Intensive tutorial: http://www.h5edu.cn/htm/step/h5edu_44.html

JavaScript Intensive Tutorial-what is a function?

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.