Basics of using JavaScript functions in basic tutorials

Source: Internet
Author: User
Tags function definition

A function is a set of reusable code that can be invoked anywhere in the program. This eliminates the need to write the same code again and again. This will help programmers write modular code. You can divide large programs into small, manageable features.

Like any other advanced programming language, JavaScript supports the features of all necessary write-modular code usage functions.

Must have seen such alert () and write () in the previous chapter function. We use these features again and again, but they have been written in core JavaScript only once.

JavaScript allows us to write our own functions, and this section describes how to write your own functions in JavaScript.
function Definition:

We used the function before, so we need to define a function. The most common way to qualify a JavaScript function is by using the function keyword, followed by a unique name for the functions, a list of arguments (which may be empty), and a block of statements surrounded by braces. The basic syntax looks like this:

<script type= "Text/javascript" >
<!--
function functionname (parameter-list)
{
 statements
}
-->
</script>

Instance:

A simple function that does not take any arguments called SayHello, defined here:

<script type= "Text/javascript" >
<!--
function SayHello ()
{
  alert ("Hello there");
}
//-->
</script>

Call a function:

To call a function in a script, you need to write the name of the function as follows:

<script type= "Text/javascript" >
<!--
SayHello ();
-->
</script>


function Arguments:

So far, we've seen the function without arguments. But there is a facility to pass different parameters, while calling a function. These parameters allow you to capture and process within a function any of these parameters to complete.

A function can have multiple arguments separated by commas.
Instance:

Let's do some modification to the SayHello function. This time, it will use two parameters:

<script type= "Text/javascript" >
<!--
function SayHello (name, age)
{
  alert (name + ' is ' + Age + "years old.");
-->
</script>

Note: We use the + operator to concatenate strings and numbers together. JavaScript doesn't mind numbers plus strings.

Now, we can call this function as follows:

<script type= "Text/javascript" >
<!--
SayHello (' Zara ', 7);
-->
</script>


Return statement:

A JavaScript function can have an optional return statement. This is required if you want to return a value from a function. This statement should be the last statement of the function.

For example, you can pass two numeric arguments to a function, and you can return from the function to the calling program multiplication value.
Example:

This function has two parameters that connect it to the calling program to return the composition:

<script type= "Text/javascript" >
<!--
function concatenate (a)
{var full
  ;

  Full = i + last;
  return full;
}
-->
</script>

Now, we can call this function as follows:

<script type= "Text/javascript" >
<!--
  var result;
  result = CONCATENATE (' Zara ', ' Ali ');
  alert (result);
-->
</script>

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.