Reputation: 379
So I was wondering, if I run this code:
incrementionFunc(num1, num2) {
print(num1+num2);
}
void main() {
incrementionFunc(2.3,3);
}
I get no issues, and it prints out 5.3
. What's the difference between not specifying the type for the variables num1, num2
, and writing out var
for example (var num1, var num2
)?
Thanks in advance!
Upvotes: 1
Views: 810
Reputation: 3862
the type is dynamic
if you don't specify it
meaning that this will compile but you'll get a runtime error:
void main() {
incrementionFunc(2.3, '1');
}
As for var
, placing it in method signature is redundant since it's the default as well.
As such, this will compile and won't throw an error:
incrementionFunc(var num1, num2) {
num1 = 12;
num2 = 13;
print(num1+num2);
}
void main() {
incrementionFunc(2.3, "1");
}
You will get different behavior if you replace var
with final
:
incrementionFunc(final num1, num2) {
num1 = 12; // Compilation error: The final variable 'num1' can only be set once.
num2 = 13;
print(num1+num2);
}
Upvotes: 1