JAVA SCRIPT DAY 9 ( Function, return Statement)

DAY - 9

JAVA SCRIPT FUNCTION

Ø  A Javascript function is a block of code designed to perform a particular task.
Ø  A function is a group of reusable code which can be called anywhere in your program. This eliminates the need of writing the same code again and again. It helps programmers in writing modular codes.
Ø  Functions allow a programmer to divide a big program into a number of small and manageable functions.
Ø  JavaScript function is executed when something invokes (calls) it.

 

JavaScript Function Syntax

A JavaScriptfunction is defined with the function keyword, followed by a name(unique function name), a list of parameters (that might be empty), and a statement block surrounded by curly braces.

Syntax

The basic syntax is shown here.
<scripttype="text/javascript">
<!--
functionfunctionname(parameter-list)
{
statements
}
//-->
</script>

Example

It defines a function called sayHello that takes no parameters −
<scripttype="text/javascript">
<!--
functionsayHello()
{
alert("Hello there");
}
//-->
</script>

Function Parameters
Function may have parameters and these different parameters are passed while calling a function.
These passed parameters can be captured inside the function and any manipulation can be done over those parameters.
A function can take multiple parameters separated by comma.

Example

The sayHello function takes two parameters.
<html>
<head>

<scripttype="text/javascript">
functionsayHello(name, age)
{
document.write(name +" is "+ age +" years old.");
}
</script>

</head>
<body>
<p>Click the following button to call the function</p>

<form>
<inputtype="button"onclick="sayHello('Zara',7)"value="Say Hello">
</form>

<p>Use different parameters inside the function and then try...</p>
</body>
</html>

 

The return Statement

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

Example

It defines a function that takes two parameters and concatenates them before returning the resultant in the calling program.
<html>
<head>

<scripttype="text/javascript">
function concatenate(first, last)
{
var full;
full= first + last;
return full;
}

functionsecondFunction()
{
var result;
result= concatenate('Zara','Ali');
document.write(result );
}
</script>

</head>

<body>
<p>Click the following button to call the function</p>

<form>
<inputtype="button"onclick="secondFunction()"value="Call Function">
</form>

<p>Use different parameters inside the function and then try...</p>

</body>
</html>

 

Note:
Ø  In JAVASCRIPT, functions are objects.
Ø  JAVASCRIPT functions have properties and methods.
Ø  In JAVASCRIPT, functions can be used as variables.
Ø  Variables declared within a JAVASCRIPT function, become LOCAL to the function. The local variables are deleted when the function is completed.
Ø  Variables declared outside a JAVASCRIPT function, become GLOBAL to the function. Global variables are deleted when you close the page.

No comments:

Post a Comment

Give your valuable feedback

Topic :Software & Types, Subject: Computer Fundamental Notes for CSJM University Kanpur(for different courses like BBA, BCA, etc..)

Software Software refers to the programs, data, and instructions that enable a computer or other digital device to perform specific tasks or...