Home > Web Front-end > JS Tutorial > Basic tutorial on using JavaScript functions_Basic knowledge

Basic tutorial on using JavaScript functions_Basic knowledge

WBOY
Release: 2016-05-16 15:56:50
Original
1326 people have browsed it

A function is a reusable set of code that can be called 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 a large program into a few small and manageable features.

Like any other high-level programming language, JavaScript supports all the features necessary to write modular code using functions.

You must have seen the alert() and write() functions in the previous chapters. 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 will introduce how to write our own functions in JavaScript.
Function definition:

We used functions before, so we need to define a function. The most common way to qualify a function in JavaScript is by using the function keyword, followed by a unique function name, a (possibly empty) argument list, and a block of statements surrounded by braces. The basic syntax is as follows:

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

Copy after login

Example:

A simple function that takes no parameters called sayHello is defined here:

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

Copy after login

Call a function:

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

<script type="text/javascript">
<!--
sayHello();
//-->
</script>

Copy after login


Function parameters:

So far we have seen that functions take no parameters. But there is a facility to pass different parameters while calling a function. Anything that can be done with these parameters can be captured and processed inside the function.

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

Let’s make some changes to the sayHello function. This time, it takes two parameters:

<script type="text/javascript">
<!--
function sayHello(name, age)
{
  alert( name + " is " + age + " years old.");
}
//-->
</script>

Copy after login

Note: We use the operator to concatenate both 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>

Copy after login


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, then you can expect to return from the function the multiplied value in the calling program.
Example:

This function has two parameters, which are combined in the calling program to return the result:

<script type="text/javascript">
<!--
function concatenate(first, last)
{
  var full;

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

Copy after login

Now, we can call this function as follows:

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

Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template