...

View Full Version : function parameters question



WA
09-25-2003, 01:09 PM
Hi:
I was wondering, lets say I define a function with 3 parameters:


function animate(x, y, speed){
if (speed=2)
//do this
}

When I invoke it, is it officially permitted to only set some of the paramaters, like so:

animate(20, 30)

In the above, speed is missing, since I don't need it for this particular invocation. Is this valid and supported by functions in JavaScript (so far no errors in browsers I've tested it in)? In the past I've always passed in '' for unused parameters, though it is a sore eye.

Thanks,

liorean
09-25-2003, 01:46 PM
I think you should do a test like this instead:

if(typeof speed!='undefined')

to detect if speed is present. You could also ask for the length of the arguments array.

You only need to pass '' (or null, or even better undefined (use void(0) for backward compatibility)) when you want to leave one argument out but want to set an argument after it.

As for your if(speed=2), it's an assignment. It'll always set speed to 2, and it'll always evaluate to true. Why not do like this instead:

var speed=typeof speed!='undefined'?speed:2; // Set speed to (the argument) speed unless that argument is undefined, when you set it to 2 instead.

Roy Sinclair
09-25-2003, 03:02 PM
The arguments passed to a function are found in an array named "arguments" and therefore the number of arguments can be found using "arguments.length". You can therefore declare a function without any explicit arguments and then retrieve them into local variables as needed (perhaps using techniques like Liorean suggests).



function a()
{
alert(arguments.length)
}
a(1,2,3,4,5,6)


This is very useful if you want to create a function where it can be passed a variable number of arguments like a "hide" function where you can pass it any number of objects you'd want the function to hide at the same time.

Vincent Puglia
09-25-2003, 04:37 PM
Hi,

To expand on Roy's comments/functions:

function a()
{
len = arguments.length;
switch (len)
{
case 2:
alert('Area = ' + (arguments[0] * arguments[1]) )
break;
case 1:
alert('Area = ' + (3.14 * (arguments[0] * arguments[0]) ) )
break;
}
}
a(1,2)
a(4)


Vinny

cheesebag
09-25-2003, 06:37 PM
Interesting read:

http://www.faqts.com/knowledge_base/index.phtml/fid/330

Vincent Puglia
09-25-2003, 10:12 PM
Yep. basic function overloading :)

Vinny

liorean
09-25-2003, 10:20 PM
Not so much function overloading (for which static programming languages are needed) as dynamic functions. The determination of what to do is done inside one and the same function, and that function can only exist in one copy, though it may allow a variable number of attributes and thus act as an overloaded function would.

Vincent Puglia
09-25-2003, 10:23 PM
Hi liorean,

You're right.
Unfortunately, I tend to be a 'if it walks like a duck...' kind of guy, so for me -- it's overloading :)

Vinny

liorean
09-25-2003, 10:28 PM
You should have a look at some LISPs - those where data isn't differentiated from functions and where the entire concept of data types is lost in the Right Way To Do It™ idea. The kind of language where there exists only three things: symbols, lists, and atoms.

Vincent Puglia
09-25-2003, 10:36 PM
a look at some LISPs
about 20 years ago, I fiddled with some, along with a few others -- pl/c, cobol, pascal, that one for 'real world' hookups, the turtle one for kids


Right Way To Do It™ idea
is that really trademarked? too much, either way :D


In spite of it all -- or because of it all -- I still like to think of it as overloading

Vinny

liorean
09-25-2003, 10:51 PM
There's a long living standard of adding a trademark sign on The Right Way, The Wrong Way, The Best and a lot of other terms like that, so I thought it appropriate...

Vincent Puglia
09-25-2003, 10:53 PM
Guess it is (I use quotes) and amusing as well

Vinny

WA
09-25-2003, 11:46 PM
Thanks people for all the great responses. I'm aware of the arguments array (even wrote a tutorial on it: http://www.javascriptkit.com/javatutors/rfunction.shtml ), so my question is specifically if I wanted to define a function with say 3 parameters by default, how "backward compatible" is it to invoke this function with a different number of parameters (atmost 3 of course), ie:

animate(20, 30)
animate(20)

Are both of the above valid in all major browsers, how these parameters are processed within the function not withstanding?

Vincent Puglia
09-26-2003, 01:06 AM
Hi WA -- (George, right?),

I've been using arguments since IE4 & NN4.5 If you define it with 3 and only deliver 1 or 2 -- make sure you handle the discrepancies -- either with switch (arguments.length) or liorean's if (typeof ..)

Vinny

liorean
09-26-2003, 06:54 AM
You can safely depend on support from JavaScript version 1.2 and on. (versions 4.0 and up of the two) I believe the typeof operator was not in the initial version of JavaScript, but I'm not entirely sure about that. As for the arguments array, it's supported earlier in nn, but added in ie4. Note that on this point, the MSDN compatibility tables don't agree with me, though.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum