Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> JavaScript >> Snippet

12 Functions


defining a function ------------------------------

function MyFunction (arg1, arg2, arg3)  //all arguments are call by reference (cannot change global value)
   { Block }

function MyFunction ()   //parentheses are required even if it has no arguments
   { Block }

function MyWriteFunction (whattoprint)
   {
    document.write whattoprint
     return "my answer"    // function returns value to right of 'return' statement (undefined if not used, nor specified)
   }

arguments ------------------------------------------
- arguments are declared automatically - don 't have to use ' var ' to declare them
- if all needed arguments are not passed, the function assigns 'undefined' to them
- are passed by value. original variable is not changed if argument is changed within function
- arguments can be objects


return values --------------------------------------
return 6   // functions return value
return true   // function return value
return MyVar  // function returns value


To call a function ---------------------------------
X = MyFunction()    // X is assigned the return value of MyFunction()
MyFunction()    // return value of MyFunction() is discarded
MyFunction (arg1, arg2, arg3)       //passed parameters can be arrays (don't need () in argument list)


Built - in functions ---------------------------------
1. isNaN
2. eval
3. parseInt
4. parseFloat
5. escape
6. unescape