...

View Full Version : Declaring functions without defining



me'
10-11-2003, 04:28 PM
Just a quick question - can you declare functions without defining them, like in C++?

eg:

function foo(var firstarg, var secondarg);

Then another function can call foo:

foo(1,2);

And foo is defined later down the page:

function foo(var firstarg, var secondarg) { return firstarg+secondarg }

BrainJar
10-11-2003, 07:08 PM
No, that would generate an error. Fortunately, unlike C/C++, there's no need to declare functions before they are called in the source with JavaScript. This is perfectly legal:

foo(1,2);

...

function foo(var firstarg, var secondarg) { return firstarg+secondarg }

me'
10-11-2003, 07:09 PM
Sadly, that _seems_ to produce an error in IE. I will have another look at my code.

nolachrymose
10-11-2003, 08:11 PM
function foo(firstarg, secondarg) { return firstarg+secondarg; }

Hope that helps!

Happy coding! :)

Vincent Puglia
10-12-2003, 06:31 AM
Hi,

javascript may not have typing, but it does have arguments:

x = foo(1,2);
alert(x)
x = foo(1,2,3)
alert(x)



function foo()
{
var retVal = 0;
var argc = arguments.length;
var argv = arguments;
for (var i = 0; i < argc; i++)
retVal += argv[i]
return (retVal)
}

Vinny

me'
10-12-2003, 11:03 AM
Originally posted by nolachrymose

function foo(firstarg, secondarg) { return firstarg+secondarg; }

Hope that helps!

Happy coding! :)

Sorry, was a typo in my reply... that's what I've got my functions like in my page.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum