Home arrow JavaScript arrow Page 2 - Introducing JavaScript Functions and Loops
JAVASCRIPT

Introducing JavaScript Functions and Loops


Today, you will learn about functions from a man who can barely function, because it is 8:48 in the morning and my soda has yet to kick in. I would say wait for it, but I know you are so excited to read the rest of this article that you can't function. And if you can't function, and I can't function, how is JavaScript gonna function? Well, you'll have to keep reading to find out.

Author Info:
By: James Payne
Rating: 4 stars4 stars4 stars4 stars4 stars / 7
November 12, 2007
TABLE OF CONTENTS:
  1. · Introducing JavaScript Functions and Loops
  2. · Putting the Fun in Your Functions
  3. · Loops
  4. · While Loop

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Introducing JavaScript Functions and Loops - Putting the Fun in Your Functions
(Page 2 of 4 )

Before I show you how to write a function and call one, let's go over a few rules. First, you can place a function in the head or body section and even in an outside .js file. You can call the function from any place within your page, and even from other pages in the event that you have stored an external .js file.

As stated before, functions hold reusable code that users call by performing an action. Take a look at this example:


<html>

<head>


<script type="text/javascript">

function firstfunction() //This is how you declare a function

{

alert("Look at me! I'm a function!")

}

</script>

</head>

<body>

<form>

<input type="button"

onclick="firstfunction()" //clicking on the button calls the function

value="Click for Function">

</form>

</body>

</html>

In the above example, we wrote a program to create a pop-up button that then declares: Look at me! I'm a function! Of course the button isn't really a function, more the result of one, but you get the drift. Had we not enclosed our code in the function, it would execute when the page loaded, and that's it. Unless the user refreshed the page, it would not happen again.

Instead, however, we did create a function, and we made a button for the user to click and call the function as often as they would like. As you can see by the above code, the way to declare a function is to type function nameoffunction(), followed by


{

your code

}

Note that if you are going to use variables for your function, and those variables are to be used only in that function, you must declare them like so:


function nameoffunction(variable_one, variable_two, etc)

Also note that the word “function” must be in lowercase letters and that the function's name is case-sensitive.

Here is an example of a function that passes a value to a variable and then uses that variable.


<html>

<head>

<script type="text/javascript">

function varfunction(complex) //a function with a var named complex

{

alert(complex)

}

</script>

</head>

<body>

<form>

<input type="button"

onclick=varfunction(2+1) //assigns value to var complex

value="Complex Math: What is 2+1?"

</form>

</body>

</html>

The above code creates a function named varfunction and a variable named complex. It then creates a button that assigns a value to the variable complex and calls the function.

The value of the variable in this case is the equation 2+1. When the user clicks the button, a pop-up appears that gives the result of 2+1, which of course is 3.

We can of course have the function trigger when the page loads if we want. Maybe you have certain text that you wish to appear across multiple pages. While you would have to create a .js file for that, here is how you would write a function that would return some text on a page when the page loads:


<html>

<head>


<script type="text/javascript">

function myFunction()

{

return ("Harry Potter Fan Fiction is for Nerds.")

}

</script>

</head>

<body>


<script type="text/javascript">

document.write(myFunction())


</script>

</body>

</html>

Here we have placed the JavaScript in the head section of our page. We have also used a new statement, the Return statement. The Return statement specifies the value a function is to return. The above code stores the value “Harry Potter Fan Fiction is for Nerds.” It then prints the value to the web page.

Next: Loops >>

blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials