Skip to main content
Known Participant
May 9, 2009
Answered

Passing function parameters in a non-sequential fashon

  • May 9, 2009
  • 1 reply
  • 1536 views

Consider the function below:

function personal(Name:String,age:int,city:String):void
{
   trace(Name+" is "+age+ " years old and lives in "+city+ ".")
}

The parameters can only(???) be passed in the order they are declared in the function as shown below.

personal("John",40,"London"0)

If the order is changed as: personal("London",John", 40), there will obviously be a type related error (position 2 parameter is an integer).

Is there a way of passing function parameters as: personal(city="London",Name=John",age= 40)?

That way one does not have to worry about the 'order' in which the parameters are passed.

This topic has been closed for replies.
Correct answer kglad

Done. Thanks. But how do I keep 'type checking'? For example personal({age:40,city:"London",name:40}) will output: 40  is 40 years old and lives in London.

If only type could be checked, it would easy to trouble shoot and spot the error.

function personal(obj:Object)
{
   trace(obj.name+" is "+obj.age+ " years old and lives in "+obj.city+ ".")
}

personal({name:"John",city:"London",age:40})
personal({age:40,city:"London",name:"John"})


you can use:

obj.age instanceof Number

obj.name instanceof String

1 reply

kglad
Community Expert
Community Expert
May 9, 2009

you can pass an object.

MwalimoAuthor
Known Participant
May 9, 2009

That is too deep for me? Please elaborate.

Thanks

Michael Borbor
Inspiring
May 9, 2009

You could pass an object as the parameter to your function, and fill this

object in any way you want. For instance you could populate an object like

this

var myObj:Object;

myObj.name="";

myObj.lastName="";