- Home /
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?
Answer by Statement · Apr 05, 2011 at 01:15 PM
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...
Sorry, but I gotta say: clai$$anonymous$$g that functions with several arguments are "generally bad practice" strikes me as an immense overstatement, given that you'll find these functions in pretty much any software code out there -- including a huge number of core Unity functions! If one wants to argue against this practice or urge restraint in its use, fine... but I'm so tired of reading claims on web forums that some developer's preferred approach to coding is actually THE widely accepted "best practice" and that other approaches are widely discredited when that's just clearly not the case. And I don't think relatively new developers should be worrying about writing functions that have two or three arguments when these can be very useful and there are so many other practices that are much more likely to break code.
Hi @Statement: could you give me the best particle about "clean code", im new i have a function with 6 arguments, i cant control them or decrease arguments my function have 3 loops, many if statements This is my Function:
public bool BetweenPokes(int rowOfColSmall, int colSmall, int rowOfColBig, int colBig, bool[][] grid)
{
bool betweenPokes = false;
for (int colCurr = colSmall; colCurr <= colBig; colCurr++)
{
if (grid[rowOfColSmall][colCurr])
{
if (rowOfColSmall < rowOfColBig)
{
for (int rowCurr = rowOfColSmall; rowCurr <= rowOfColBig; rowCurr++)
{
if (!grid[rowCurr][colCurr])
break;
if (rowCurr == rowOfColBig)
{
for (int colSubs = colCurr; colSubs <= colBig; colSubs++)
{
if (!grid[rowOfColBig][colSubs])
break;
if (colSubs == colBig)
{
betweenPokes = true;
}
}
}
}
}
if (rowOfColSmall > rowOfColBig)
{
for (int rowCurr = rowOfColSmall; rowCurr >= rowOfColBig; rowCurr--)
{
if (!grid[rowCurr][colCurr])
break;
if (rowCurr == rowOfColBig)
{
for (int colSubs = colCurr; colSubs <= colBig; colSubs++)
{
if (!grid[rowOfColBig][colSubs])
break;
if (colSubs == colBig)
{
betweenPokes = true;
}
}
}
}
}
}
else
break;
if (betweenPokes)
break;
}
return betweenPokes;
}
Answer by fredukita · Dec 19, 2012 at 12:12 AM
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);
Answer by Eric5h5 · Apr 05, 2011 at 09:17 AM
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.
However, Robin want to omit one parameter in the middle, and that won't work.
$$anonymous$$aybe 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 :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613120817im_/https://answers.unity.com/themes/thub/images/avi.jpg)