Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by robinking · Apr 05, 2011 at 09:03 AM · function

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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?

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robinking · Apr 05, 2011 at 10:20 PM 0
Share

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

avatar image sumeetkhobare · Sep 23, 2013 at 04:23 PM 0
Share

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

avatar image monotoan · Jan 05, 2017 at 04:26 PM 0
Share

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.

avatar image hdtnl · Jan 05, 2017 at 05:35 PM 0
Share

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;
     }
avatar image
1

Answer by Mike 3 · Apr 05, 2011 at 09:11 AM

Not in javascript/unityscript

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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);

  

   
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Statement · Apr 05, 2011 at 01:04 PM 0
Share

That's something new for me.

avatar image Statement · Apr 05, 2011 at 01:05 PM 1
Share

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

avatar image kuom · Jul 06, 2014 at 12:23 AM 0
Share

$$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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Material doesn't have a color property '_Color' 4 Answers

How can i call a GUI function? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges