PDA

View Full Version : Combining Functions with Parameters?



Graeme Hackston
06-19-2002, 02:34 AM
I have 3 functions in which 75% of their statements are the same. Can I combine them into 1 function using parameters? If so, how do I call a parameter from a link and define them in the function?


I believe the function and links that call it would look something like this:

function One(WhatDoIPutHere) {

all common statements here;

if (Link1) {
do stuff;
}

if (Link2) {
do stuff;
}

if (Link3) {
do stuff;
}
}

<a href="javascript:void(0);" onmousedown="One('Link1')">Link1</a>

<a href="javascript:void(0);" onmousedown="One('Link2')">Link2</a>

<a href="javascript:void(0);" onmousedown="One('Link3')">Link3</a>


Thanks for your help.

JohnKrutsch
06-19-2002, 02:48 AM
Here is one way you can do it:

function One(num) {

all common statements here;

if (num=='Link1') {
do stuff;
}

if (num=='Link2') {
do stuff;
}

if (num=='Link3') {
do stuff;
}
}

Graeme Hackston
06-19-2002, 02:52 AM
Thanks John.

I understand what == does but what is "num"? Is it an abreviation for number?

JohnKrutsch
06-19-2002, 05:31 AM
num was just an arbitrary variable name I choose. In my mind it did mean number but it really could be anything you want. Choose a vairbale name that makes sense to you. The variable name will represent the parameter that you passed in. It could just as easily been:


function One(fredtheVariableIamIam) {

all common statements here;

if (fredtheVariableIamIam=='Link1') {
do stuff;
}

if (fredtheVariableIamIam=='Link2') {
do stuff;
}

if (fredtheVariableIamIam=='Link3') {
do stuff;
}
}

Graeme Hackston
06-19-2002, 05:36 AM
LOL thanks John.