New Post has been published on http://webgoodmorning.com/learn-javascript-function/
Learn more about JavaScript Function
Function is a group of JavaScript statements that we declare once in our script and call over and over again.
Features of JavaScript Function
JavaScript is created by function keyword, followed by function name and followed by parentheses ().
Function names can contain only letters, digits, underscores, and dollar signs.
Parentheses contain parameter names separated by commas – (parameter1, parameter2, parameter3, ….)
Within curly brackets write code to execute function.
function name( parameter1, parameter2, parameter3, parameter4, …. )
statements
return value
function calculateResult ( maths, english, science)
var totalMarks = maths + english + science;
return totalMarks;
alert ( “Total Marks are:” + calculateResult(70, 80, 90) );
After declare a function, we call a function by specifying the name of function, followed by an open parenthesis, comma-delimited parameters, and a closing parenthesis.
alert ( “Total Marks are:” + calculateResult(70, 80, 90) );
When JavaScript reaches a return statement, the function will stop executing. We use return statement to return a value from a function.
Syntax for the return keyword
function calculateResult ( maths, english, science)
var totalMarks = maths + english + science;
return totalMarks;
// Now the function is defined, so it can be called
document.write( “Total Marks are:” + calculateResult(70, 80, 90) );
In above example, document..write() method call the calculateResult() function. And the calculateResult() function returns the value of the totalMarks variable, document..write() method displays totalMarks on the Web page.