−JavaScript function provide a way to organize scripts and promote reuse. Rather than create longs sections of script scattered throughout an HTML page the script is instead organized into logical groups. JavaScript functions are simple to use, in fact you only need to do two things with a function, declare it and call it.
+JavaScript functions provide a way to organize scripts and promote re-use. Rather than create long sections of script scattered throughout an HTML page, the script is instead organized into logical groups. JavaScript functions are simple to use, in fact you only need to do two things with a function, declare it and call it.
== Declaring and Calling a JavaScript Function ==
== Declaring and Calling a JavaScript Function ==
</pre>
</pre>
−The ''name'' specifies how we will refer to this function when we call it. The ''arguments'' specify values that are passed through to the function to be processed. The ''statements'' section represents the body of the function that performs the processing. The optional ''return'' statement allows a value to be returned the section of scripted that called the function (for example to return a status or the result of a calculation.
+The ''name'' specifies how we will refer to this function when we call it. The ''arguments'' specify values that are passed through to the function to be processed. The ''statements'' section represents the body of the function that performs the processing. The optional ''return'' statement allows a value to be returned to the section of scripted which called the function (for example to return a status or the result of a calculation).
The following simple example shows a function defined in the ''<head>'' section of an HTML page and a ''call'' to this function in the ''<body>'':
The following simple example shows a function defined in the ''<head>'' section of an HTML page and a ''call'' to this function in the ''<body>'':
</pre>
</pre>
−In this example the function is called multiple times, passing through arguments that are then used in the function to construct a string to be displayed in the alert dialog. To achieve this without functions it would ne necessary to repeat the script in the function 3 times over in the body. Clearly placing the script ina function and re-using it over and over is a much more efficient approach.
+In this example the function is called multiple times, passing through arguments that are then used in the function to construct a string to be displayed in the alert dialog. To achieve this without functions it would ne necessary to repeat the script in the function 3 times over in the body. Clearly, placing the script in a function and re-using it over and over is a much more efficient approach.
Next we need to look at how a function might return a value.
Next we need to look at how a function might return a value.
</pre>
</pre>
−The above example passes 10 and 20 through to the addValues function. The addValues function adds these two values together and returns the result. The assigment assigns the result to the variable ''result'' which is then used to construct a srting that is written to the HTML page.
+The above example passes 10 and 20 through to the addValues function. The addValues function adds these two values together and returns the result. The assignment operator assigns the result to the variable ''result'' which is then used to construct a string which, in turn, is written to the HTML page.
Exception encountered, of type "Error"
[8b6d6879] /index.php?diff=prev&oldid=1335&title=Understanding_JavaScript_Functions Error from line 434 of /var/www/techotopia/includes/diff/DairikiDiff.php: Call to undefined function each()
Backtrace:
#0 /var/www/techotopia/includes/diff/DairikiDiff.php(544): DiffEngine->diag()
#1 /var/www/techotopia/includes/diff/DairikiDiff.php(344): DiffEngine->compareSeq()
#2 /var/www/techotopia/includes/diff/DairikiDiff.php(227): DiffEngine->diffLocal()
#3 /var/www/techotopia/includes/diff/DairikiDiff.php(721): DiffEngine->diff()
#4 /var/www/techotopia/includes/diff/DairikiDiff.php(859): Diff->__construct()
#5 /var/www/techotopia/includes/diff/DairikiDiff.php(980): MappedDiff->__construct()
#6 /var/www/techotopia/includes/diff/TableDiffFormatter.php(194): WordLevelDiff->__construct()
#7 /var/www/techotopia/includes/diff/DiffFormatter.php(140): TableDiffFormatter->changed()
#8 /var/www/techotopia/includes/diff/DiffFormatter.php(82): DiffFormatter->block()
#9 /var/www/techotopia/includes/diff/DifferenceEngine.php(881): DiffFormatter->format()
#10 /var/www/techotopia/includes/diff/DifferenceEngine.php(797): DifferenceEngine->generateTextDiffBody()
#11 /var/www/techotopia/includes/diff/DifferenceEngine.php(728): DifferenceEngine->generateContentDiffBody()
#12 /var/www/techotopia/includes/diff/DifferenceEngine.php(662): DifferenceEngine->getDiffBody()
#13 /var/www/techotopia/includes/diff/DifferenceEngine.php(632): DifferenceEngine->getDiff()
#14 /var/www/techotopia/includes/diff/DifferenceEngine.php(453): DifferenceEngine->showDiff()
#15 /var/www/techotopia/includes/page/Article.php(797): DifferenceEngine->showDiffPage()
#16 /var/www/techotopia/includes/page/Article.php(508): Article->showDiffPage()
#17 /var/www/techotopia/includes/actions/ViewAction.php(44): Article->view()
#18 /var/www/techotopia/includes/MediaWiki.php(490): ViewAction->show()
#19 /var/www/techotopia/includes/MediaWiki.php(287): MediaWiki->performAction()
#20 /var/www/techotopia/includes/MediaWiki.php(714): MediaWiki->performRequest()
#21 /var/www/techotopia/includes/MediaWiki.php(508): MediaWiki->main()
#22 /var/www/techotopia/index.php(41): MediaWiki->run()
#23 {main}