# Integer return type not typecasted only when returning from function

**URL:** <https://forum.crystal-lang.org/t/integer-return-type-not-typecasted-only-when-returning-from-function/3402>\
**Category:** Help & Support\
**Created:** [June 20, 2021, 12:29am UTC](https://forum.crystal-lang.org/t/integer-return-type-not-typecasted-only-when-returning-from-function/3402 "2021-06-20T00:29:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vgramkris](https://avatars.discourse-cdn.com/v4/letter/v/7ea924/32.png) [@vgramkris](https://forum.crystal-lang.org/u/vgramkris)\
**Post date:** [June 20, 2021, 12:29am UTC](https://forum.crystal-lang.org/t/integer-return-type-not-typecasted-only-when-returning-from-function/3402/1 "2021-06-20T00:29:59Z")

</div>

I am trying some code snippets with Crystal 1.0.0 and observing this. I am not sure if this is expected behavior, if yes then any detail would be helpful to understand.

Why is 0 not getting typecasted to Float64 only while returning from a function, but assignment & as an argument is being accepted?

```auto
  1 def func1 ( a : Float64 )
  2 # Argument is accepted as Float64
  3 a
  4 end
  5 
  6 def func2 : Float64
  7 # Return type as Float64 is not accepted; Compilation error for func2
  8 0
  9 end
 10 
 11 c : Float64 = 0 # Assignment is accepted as Float64
 12 puts func1(0)
 13 puts func2

```

Compilation error:

```auto
In code2.cr:6:13

 6 | def func2 : Float64
                 ^------
Error: method top-level func2 must return Float64 but it is returning Int32

```

My crystal version - Debian Linux

```auto
Crystal 1.0.0 [dd40a2442] (2021-03-22)
LLVM: 10.0.0
Default target: x86_64-unknown-linux-gnu

```

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [June 20, 2021, 12:45am UTC](https://forum.crystal-lang.org/t/integer-return-type-not-typecasted-only-when-returning-from-function/3402/2 "2021-06-20T00:45:39Z")

</div>

Because that’s how the language works. It’s easy to see that you pass a number literal to a call. It’s harder to do that when a method can have multiple points where a value is returned.

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [June 20, 2021, 12:46am UTC](https://forum.crystal-lang.org/t/integer-return-type-not-typecasted-only-when-returning-from-function/3402/3 "2021-06-20T00:46:40Z")

</div>

That said, it might be a nice enhancement. But I’m not sure. It might be misleading if someone reads the code and see an integer being returned, and the return type is far away.

---

<div class="post-metadata">

**Author:** ![vgramkris](https://avatars.discourse-cdn.com/v4/letter/v/7ea924/32.png) [@vgramkris](https://forum.crystal-lang.org/u/vgramkris)\
**Post date:** [June 20, 2021, 12:49am UTC](https://forum.crystal-lang.org/t/integer-return-type-not-typecasted-only-when-returning-from-function/3402/4 "2021-06-20T00:49:16Z")

</div>

Thanks for the clarification. Coming from C/C++ world, I was expecting same rules for arguments vs return types, that the language would take care of typecasting. Just that I saw that rules seems differently applied in this usecase. May be a consistent behavior would be more logical, just my humble opinion.

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [June 20, 2021, 12:49am UTC](https://forum.crystal-lang.org/t/integer-return-type-not-typecasted-only-when-returning-from-function/3402/5 "2021-06-20T00:49:45Z")

</div>

“that’s how it works” wasn’t a nice answer. What I really meant is that right now it’s expected behavior :-)

---

<div class="post-metadata">

**Author:** ![vgramkris](https://avatars.discourse-cdn.com/v4/letter/v/7ea924/32.png) [@vgramkris](https://forum.crystal-lang.org/u/vgramkris)\
**Post date:** [June 20, 2021, 12:50am UTC](https://forum.crystal-lang.org/t/integer-return-type-not-typecasted-only-when-returning-from-function/3402/6 "2021-06-20T00:50:51Z")

</div>

Not an issue 🙂  
Appreciate your timely responses, very helpful for newbies like me. Thanks for your efforts!
