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
0
Question by MountDoomTeam · Nov 04, 2012 at 09:10 AM · functiondatatypedeclare

why function datatype i.e. function foo() : int {

I just found an answer on the forum where the guy defines the data type for a function, and returns a value at the end of the function,it goes like this:

 function MaxValue (intArray : int[]) : int {
     var max = intArray[0];
     for (i = 1; i < intArray.Length; i++) {
        if (intArray[i] > max) {
          max = intArray[i];
        }
     }
     return max;
 }

what is the point in declaring the function datatype? is it related to the return command at the end of the function?

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

2 Replies

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

Answer by phodges · Nov 04, 2012 at 09:31 AM

Efficiency. As you say, it is specifying what sort of value will be returned when the function completes. Javascript, out of the box, will let you return any data type you like or none at all. The following is an example of valid code:

 var value : int;
 
 function Billy() {
 
     if (0 == value){
         // returns a string
         return "fish";
     }else if (1 == value){
         // returns an int
         return 42;
     }else{
         // return nothing at all
         return;
     }
 }

Absolutely nothing wrong with such code without the constraint that you've highlighted. However, if you can be more specific about the type of data you plan to return then the compiler warn you if you've made a mistake in the value you return and that can save you valuable time. We're all human and can benefit from checks of one form or another.

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 Eric5h5 · Nov 04, 2012 at 03:19 PM 0
Share

That's not actually correct; there are no efficiencies or optimizations to be gained by specifying the type. The compiler infers the type of the function from what you return, and having the compiler infer the type is exactly the same as specifying it yourself, as far as the compiled code goes. The only thing that specifying the type does is prevent you from returning a type that you didn't mean to return, although that's still worthwhile.

Also it's impossible not to have a return type; the code you posted is returning type Object. If you don't return a value then it will return type void.

avatar image Eric5h5 · Nov 04, 2012 at 03:44 PM 0
Share

Sorry, but no. Take a look at the CIL and you'll see. Type inference is purely a compile-time operation in Unity, and has no effect whatsoever on the compiled code, AOT or not.

avatar image phodges · Nov 04, 2012 at 04:19 PM 0
Share

Just been through the CIL and the compiler is doing a better job than I gave it credit for. Will amend my answer.

avatar image
0

Answer by Eric5h5 · Nov 04, 2012 at 03:23 PM

The point of specifying a type for a function is the same as specifying the type for a variable: it "locks in" the type, thereby preventing the compiler from inferring it. While this doesn't change the actual compiled code in any way, it can prevent coding errors resulting from accidentally returning the wrong type, and can potentially make the code easier to follow.

Comment
Add comment · Show 2 · 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 MountDoomTeam · Nov 04, 2012 at 05:23 PM 0
Share

ok thanks! i read that in javascript a function can also be a variable, like the result of the function can be written as.

          var returnedInt = function(num);
avatar image Eric5h5 · Nov 04, 2012 at 05:34 PM 0
Share

Remember that Unity does not use Javascript, it's technically Unityscript, which is much more like ActionScript3 than it is Javascript.

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

function that will return the name of the data type of a variable? 2 Answers

Can eval() declare functions (such as Update()) 0 Answers

Function triggered when scene changes? 4 Answers

gameObject.tag doesn't work as I'd expect inside a function Update 0 Answers

How do i run scripts more times 2 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