Left Quote    Nearly all men can stand adversity, but if you want to test a man's character, give him power.
- Abraham Lincoln    
Right Quote
[login] | [Register]

Learning JavaScript Basics

by: bs0d
Page: 8 of 10
(View All)


Functions can play an important role in your JavaScript programs. Functions are a set of statements that are put together for a single purpose. You create a function for blocks of code you might need to re-use throughout your program. Using functions make your program modular and flexible. Also, it will be easier to read and follow through. There are three different ways they can appear when calling them in JavaScript:
  • On a line by itself
  • Right hand side of equal sign
  • In a "document.write" statement

How they work

Ok, when a function is called upon, the control is transferred to the function and the statements in your function are then executed. Once complete, control returns to the command where you called the function or to the next line.

Return Statement

You use the return statement to return a value back to the main program. Here is how you would use the return statement:

Naming your functions

When you name your functions, you want to be sure and make the name unique. The rules are the same when naming your variables. Here I will show the examples of some functions, and the different ways they can appear.

In the code sample above, this function call is on the right hand side of the equal sign. You see how it works? When you call the function (by its name) in parenthesis put in the 2 variables that will take the place of 'a' and 'b' and the statements in between the { and } will be executed using the variables you put in it.

This is how you would call the function in a "document.write" statement. It will work the same as above, its just called differently.

This is how you could call a function where its on a line by itself. You just replace the return(); with a document.write that will print your result when you call it (if that's what you desire).

One last bit of information on functions, you can also use them to work with strings, so you're not limited just to use them with numbers or anything.


Understanding it more? The function is food, the identifier 'fruit' is the parameter. So fruit is a string used to print the multiple items. We called the function 3 times.

The possibilities for function are endless, and you can make them as complex or as simple as you wish. From adding number or bolding text to performing complex arithmetic operations and multiple text manipulations.

1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |  10  |  
Next »

No Comments for this page.

You Must be logged in or a member to comment.

Tutorial Stats

Tutorial Stats

3 Total Comments
4.66 Rating of 5 (3 Votes)


Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article



· Getting Started with Ajax
Your Article Here

"" Copyright © 2002-2020; All rights lefted, all lefts righted.
Privacy Policy