creating function with default arguments?

Is there a way of doing something like this?

function Bar( a : int = 15, b : int = 20, c : int = 25, d : int = 30 ) {
   // Do something
}

Bar( 12, 9, , 7 );

So that c is taken to be the default value of 25? (The above results in an error.) I know I can have multiple versions of the function with different argument lists, but in this case all arguments are ints so how would the function know which arguments were being supplied?

4 Answers

4

Mr. King, first off I should say that it's generally bad practice to write functions that have several arguments, for several reasons. If you want a full explanation of why, read Clean Code by Robert C. Martin.

One suggestion would be to create a struct or class that contain the data you want to pass, so you can handle default parameters in that.

// Create a class for the data, or a struct by extending System.ValueType.
class BarArgs {
    var a : int = 15;
    var b : int = 20;
    var c : int = 25;
    var d : int = 30;        
}

function Bar( data : BarArgs ) {       
   // Do something with data.a, data.b, data.c, data.d
}

var data = BarArgs();
data.a = 12;
data.b = 9;
data.d = 7;
Bar(data);

In C# you could have used initializers:

Bar(new BarArgs() {a = 12, b = 9, d = 7});

If you don't want to do that, I wouldn't make overloads in any other order than the present:

function Bar() {
    Bar(15);
} 
function Bar( a : int ) {
    Bar(a, 20);
} 
function Bar( a : int, b : int ) {
    Bar(a, b, 25);
} 
function Bar( a : int, b : int, c : int ) {
    Bar(a, b, c, 30);
} 
function Bar( a : int, b : int, c : int, d : int ) {
   // Do something
} 

Because as you already mentioned, it's not a good idea to swap order for the parameters. It's error prone and will leave you with a lot to desire.

However, if you want to change the order, you might want to create an overload with a different name.

function BarABD( a : int, b : int, d : int) {
    Bar(a, b, 25, d);
}

Then you'd call it by:

BarABD( 12, 9, 7 );


Or you could consider just forcing to deal with 4 arguments (or any subset overloads). Why do you need all this flexibility?

Thanks for the lengthy answer! I'll check out that book. And the example of using a class is an interesting one. Great!

Thank you ... very helpful... :D using javascript...

Not in javascript/unityscript

You can do it in C#:

void Bar (int a = 15) {
}

However, not only does this not work in JS, you can't even call C# functions which do this from JS.

That's something new for me.

However, Robin want to omit one parameter in the middle, and that won't work.

Maybe I should switch to c#, or boo, you assign default values for function args in Python, but I figure, it seems like a bad idea to use multiple languages. currently using all js, but I love it! Don't need to ever do that any ways :)

Another JS solution:

    public function animate(name:String,speed,fade): void {
        speed = speed || 2.0; // Default = 2.0
    	fade = fade || 1.0; // Default = 1.0
    	animation[name].speed = speed;
    	animation.CrossFade(name, fade);    
    }

Usage:

animate(idleAnimation.name,null,null);  
animate(walkAnimation.name,null,2.0); 
animate(runAnimation.name,4.0,3.0);