Lesson 5: Javascript Functions



In this lesson we will explain the javascript function. We will learn to create our own functions and then in Lesson 6 we will explore some of javascript's built in functions.


The bare bones sructure of a fuction is as follows:


function theFunctionName() {

//some code goes here;

}


Functions are used as a way to control when your code is exicuted. They are essentially data containers. The code that is put between the curly brackets will not be exicuted untill the function is called. One of the things that often is confusing in Javascript is that a function is also technically an object. A function also can have arguments. The function's arguments go in the parenthesis after the function name. If there are no aguments then you still have to use the (). There are no limit to the number of arguments that you can use but you do not want to make it too complicated. here is an example of how arguments work:


function theFunctionName(argument1,argument2,......argumentx)


working example:


Put this inside the script tags that are in the head of the your template:


function x(a,b) {

z=a+b;

document.write(z);

}


Then put this directly after the body tag:


<form>

<input type="button" value="push" onClick="x(2,3)">

</form>


The finished html page should look like this:


<html>

<head>

<title>Code Template</title>

<script type="text/javascript">

<!--

function x(a,b) {

z=a+b;

document.write(z);

}


//-->

</script>

</head>

<body>

<form>

<input type="button" value="push" onClick="x(2,3)">

</form>

<script type="text/javascript">

<!--


//-->

</script>

</body>

</html>


You may copy and paste the above code into your notepad and save it as: your.html and open it in your web browser and it will process function x when you push the button.


This lesson is continued on the next page. Just click on the "next" arrow below to continue:

                    

Copyright © 2012 VisualBuilder. All rights reserved