- Home /
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?
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.
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.
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.
Just been through the CIL and the compiler is doing a better job than I gave it credit for. Will amend my answer.
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.
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);
Remember that Unity does not use Javascript, it's technically Unityscript, which is much more like ActionScript3 than it is Javascript.
Your answer
Follow this Question
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