Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

function arguments 2

Status
Not open for further replies.

Udir

Technical User
Aug 16, 2001
111
US
If there is a function like

function funname(name)
{
statements;
}

is it possible to call the function without actually passing the function anything?

like

x = funname();

Also, aside from msdn.microsoft.com can anyone recommend a good website/tutorial on objects in JScript?
 
Yes, you can call a function without sending any parameters. However, if you reference the arguments in the function without checking to see if they're undefined, you'll get an error and the function won't function.

function func(onearg)
{
if (onearg + '' == 'undefined')
{
onearg = defaultvalue;
}
}

You can also pass more parameters to a function that you list as arguments, and then access them through the arguments array.

function func()
{
var onevar=0, twovar="twovar", threevar=false;
if (arguments.length > 0) onevar = 3;
if (arguments.length > 1) twovar = 'twovarplus';
if (arguments.length > 2) threevar = true;
//other statements
}

Lee
 
So what would happen if the argument intended to be passed was a bool and I didn't pass it so it was undefined?

function func(arg)
{
if(arg)
statement;
else
statement1;
}

var x = func();

would it return whatever statement1 actually processed? And thanks for that second tidbit of info. That really helps.
 
The function would end processing at the first statement where it tries to evaluate the undefined variable. Nothing would return, nothing would happen in the function at the point the error took place.

You'd have to use something like:

if (arg + '' != 'undefined')
{
statements;
}
else
{
elsestatements;
}

You'd have to pass it a true, nonnull, or nonzero parameter for the function to process what you wrote.


Lee
 
Ok I wrote this to test out the idea.


x=func();
y=func(65);
z=func(20);
WScript.Echo(x);
WScript.Echo(y);
WScript.Echo(z);

function func(arg)
{
if (arg + '' == 'undefined')
{
arg = 23;
}
if(arg == 65)
{
arg=19;
}
else
{
arg = 95;
}
return(arg);
}


One would expect
x=23
y=19
z=95

but rather I get 95, 19, 95
 
You're getting exactly what you wrote the function to do. Even though you assign the value of 23 to arg, in the second if statement you check the value, assign 19 to the variable if the original value is 65, and assign 95 to it in any other case, which includes if it equals 23 at the time. You'd have to use else with the second if to produce the results you want.

Lee
 
Or you could do it like this:

function func(arg)
{
if (arg + '' == 'undefined')
{
return 23;
}
if(arg == 65)
{
return 19;
}
return 95;
}

Lee
 
You were missing an "else". Try this:
Code:
x=func();
y=func(65);
z=func(20);
WScript.Echo(x);
WScript.Echo(y);
WScript.Echo(z);

function func(arg)
{
  if([red]typeof arg == 'undefined'[/red])
  {
    arg = 23;
  }
  [red]else[/red] if(arg == 65)
  {
    arg=19;
  }
  else
  {
    arg = 95;
  }
  return(arg);
}

Adam

There's no place like 127.0.0.1
 
Looks like Lee beat me to it...

Adam

There's no place like 127.0.0.1
 
There is some misconception here. If you do this:
Code:
function func(arg) {
   if (arg)
      alert("True");
   else
      alert("False");
}
func();
What you get will be the alert "False", not an error. That's because and undefined parameter is FALSE when used in a condition like that. You don't have to concatenate it with null and check for undefined.

That allows an easy way to specify defaults for omitted arguments:
Code:
myarg = arg ? arg : "default";


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Tracy,

I think part of Udir's question was how to differentiate between an argument whose value is the boolean False and an argument that was never specified. Since both would evaluate to False in a test condition, you would need to use a different method for seeing if the argument was passed or not. In this case, I believe the typeof operator, as seen in my example, would be beneficial.

Adam

There's no place like 127.0.0.1
 
The same holds true if the variable arg contained the integer 0. It would evaluate to false in the same test, and if this were on an order form where something like quantity could hold a valid value of 0 this would cause problems.

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
[banghead]
 
From DevGuru on converting values to booleans:

...if it is 0, -0, null, false, NaN, undefined, or the empty string (""), the initial value is false. Otherwise, even with the string "false", it is true.

Adam

There's no place like 127.0.0.1
 
adam101 I agree that if you want to determine if the argument had been omitted using the typeof operator is a good idea.

kaht: If you're programming an order form and you have to worry about whether the total is 0 or you forgot to pass a value to the function at all, you've got much bigger problems!

If you're REALLY concerned about omitted arguments, the mention above of the arguments array gives you the best hint for a solution:
Code:
if ( [i]funcname[/i].arguments.length == 0 ) {
   alert("You didn't pass any arguments to my function!");
}


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top