# Passing optional variables in brackets

**URL:** <https://forum.kirupa.com/t/passing-optional-variables-in-brackets/281849>\
**Category:** flash\
**Created:** [February 13, 2009, 8:17pm UTC](https://forum.kirupa.com/t/passing-optional-variables-in-brackets/281849 "2009-02-13T20:17:30Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alpha\_Remex](https://avatars.discourse-cdn.com/v4/letter/a/779978/32.png) [@Alpha\_Remex](https://forum.kirupa.com/u/Alpha_Remex)\
**Post date:** [February 13, 2009, 8:17pm UTC](https://forum.kirupa.com/t/passing-optional-variables-in-brackets/281849/1 "2009-02-13T20:17:30Z")

</div>

:megaman\_x:So check this out…

Let’s say I have a function in a class with two required parameters and two optional:

```auto

public class Person extends Sprite {
    private var haveToWork:Boolean;
    private var hoursOfSleep:Number;
    private var gotANewVideoGame:Boolean;
    private var gotAHotDate:Boolean;

    public function Person():void {
        //Constructor stuff
    }

    public function areYouHappy(haveToWork:Boolean, hoursOfSleep:Number,
                                           gotANewVideoGame:Boolean = false, 
                                           gotAHotDate:Boolean = false):void {

        //Assign to instance variables
        this.haveToWork = haveToWork;
        this.hoursOfSleep = hoursOfSleep;
        this.gotANewVideoGame = gotANewVideoGame;
        this.gotAHotDate = gotAHotDate;

    }
}

```

As you can see, this function stores information in an instance of the class… other functions will actually be used to return values.

So right now, to use this function from another class, I would have to do something like this:

```auto

var brad:Person = new Person();

brad.areYouHappy(true,8,false,true);

```

What I would rather do is to pass in optional arguments by name… now I have seen this done before and from what I understand, it would look something like this:

```auto

public class Person extends Sprite {
    private var haveToWork:Boolean;
    private var hoursOfSleep:Number;
    private var vars:Object;

    public function Person():void {
        //Constructor stuff
    }

    public function areYouHappy(haveToWork:Boolean, hoursOfSleep:Number,
                                           vars:Object):void {

        //Assign to instance variables
        this.haveToWork = haveToWork;
        this.hoursOfSleep = hoursOfSleep;
        this.gotANewVideoGame = vars.gotANewVideoGame;
        this.gotAHotDate = vars.gotAHotDate;

    }
}

```

And I think it would be called like this:

```auto

var brad:Person = new Person();

brad.areYouHappy(true,8,{gotANewVideoGame:true});

```

Now for my purposes, I have to call that function two different times:

```auto

 var brad:Person = new Person();
 
brad.areYouHappy(true,8,{gotANewVideoGame:true});
.
.
.
//Other stufff happens
.
.
.
 brad.areYouHappy(true,8,{gotAHotDate:true});
 

```

All of that setup for this question: on the second call of the areYouHappy function, how do I keep the gotANewVideoGame variable from having a false value assigned to it?

Does that make sense at all?
