Function Parameters

Parameters provide a way to pass values to a functions. Functions often have to be given additional information such as what image to change, what the message for an alert() should be, or what to write to the page in document.write(). When defining a function you may put 0 or more comma-separated parameters in the parentheses following the function name. Parameters act a lot like variables that get initialized, given an initial value, when the function is called. When you call a function, i.e. use it in a piece of code, what you put in the parentheses are called "arguments". Also, w3schools.com has some information on this.

Parameter Pointers:

Examples:

The function definition code:

<script type="text/javascript">
function noParameters(){
alert("I have no Parameters");
}
function echo1Parameter(x){
alert("I have one Parameters: "+x);
}
function echo2Parameter(x,y){
alert("I have two Parameters: " + x + ", " + y);
}
function echo3Parameter(x,y,z){
alert("I have three Parameters: " + x + ", " + y + ", " + z);
}
</script>

Code Interleaved with Results

NO parameter button code:
<button onclick="noParameters()">Click for alert</button>


One parameter button code:
<!-- Same parameter, different arguments-->
<button onclick="echo1Parameter('fred')">One Parameter: 'fred'</button>
<button onclick="echo1Parameter('greg')">One Parameter: 'greg'</button>


Two parameter button code:
<!-- Same arguments but in reverse order-->
<button onclick="echo2Parameter('fred','greg')">Two parameters: 'fred' and 'greg'</button>
<button onclick="echo2Parameter('greg','fred')">Two parameters: 'greg' and 'fred'</button>


THree parameter button code:
<!-- ...and three argmuents-->
<button onclick="echo3Parameter('fred','greg','jeb')">Three parameters: 'fred', 'greg' and 'jeb'</button>



Back to Knowledge Dump