JavaScript Functions

When some tasks have to be done again and again with different values it is always better to give a name to these set of statements and call them by this name as many times as required. JavaScript Functions are such named blocks of specific JavaScript statements. A function is always called wherever and whenever required in a script. Function call is usually associated with an event triggered by a user or some action in a script.

Characteristics of JavaScript Functions

  • A JavaScript function is a named block of JavaScript statements given within opening and closing curly parentheses.
  • The functions in JavaScript are always defined with keyword function and may have parameters.
  • The parameters are given in parentheses after the name of the function. The parameters are given as a list of parameter names separated with commas.
  • JavaScript Function parameters are optional.
  • Functions are called when some user triggered event occurs. Events occur when a user does some activity like click a button, enter some text in a textbox, move to another HTML form element etc.
  • JavaScript Functions are declared in the HEAD section of a web page. The functions in JavaScript code must be declared before they are called.

Syntax- JavaScript Functions

A JavaScript function declaration begins with keyword function. It is followed by the name of the function and the list of parameters in parenthesis and separated by commas. The function name is given following the variable naming rules of JavaScript.

function func_name([param1],[param2]….)
	Function statements

Calling JavaScript Functions

JavaScript functions can be called at different locations in a web page. A function is usually called in the body section of a webpage. The function is called with a list of argument values within parenthesis. If the function is declared without parameters the function is still called by giving name of function followed by empty parentheses. In this case parentheses are not optional.

If a function is declared in the head section of a webpage it can be called only in that specific page. If a function is declared in an external js file then such a function can be called in any webpage of a website.

return Statement

If your need is to get back the calculated/evaluated value from a function then the return statement must be given, preferably, at the end of the function.

Whenever a function call encounters a return statement, it immediately terminates. Termination results in returning the specified value of variable/ expression given after return keyword.  The returned value can be stored in a variable or displayed in the webpage or in an alert box.

The return statement can be associated with a conditional statement to terminate a function on occurrence of that condition and return the value.

Example using JavaScript Functions

<SCRIPT LANGUAGE="javascript">
function displayMsg(user,msg)
	return "<BR>Hello "+user+"!! "+msg;

function warning(user,msg)
	document.write( "<BR>Hey "+user+"!! "+msg);

function genMsg()
	return "Hello user!! welcome to";

getMsg=genMsg();//calling a function  with no arguments and store returned value in a variable
alert(getMsg);//displaying value in alert box
document.write( displayMsg("Weasel","Good morning"));//calling a function that returns a value
warning("Franny","Stop fidgetting");//calling a function that do not return a value