-2

Why this function call works? The function func(int,int) is declared as taking integers but even when calling it with double is working. Why is it so?

#include<iostream>
using namespace std;


void func(int a,int b){


cout<<"a is "<<a;
cout<<"\nb is "<<b;

}

int main(){
func(12.3,34.3);


}
rooni
  • 1,036
  • 3
  • 17
  • 33
  • 7
    Those are `double`s not `float`s and it compiles because implicit conversions between numeric types exist. You can turn up your warning if that bothers you. – nwp Feb 15 '18 at 12:28
  • what do you mean by it wont even compile? I compiled it before posting and again before writing this – rooni Feb 15 '18 at 12:32
  • 1
    See https://ideone.com/QT654x - missing `void` return type on `func`. – MSalters Feb 15 '18 at 12:33
  • If you put some effort into it [it can compile](http://coliru.stacked-crooked.com/a/0dacc29fb0699aa1). Doesn't make it legal or good though. – nwp Feb 15 '18 at 12:36

2 Answers2

6

It implicitly converts the doubles to ints. You will notice that func(12.3,34.3); prints them as 12 and 34. When you call a function your compiler looks for the function with the best matching signature to call. In this case it found void func(int, int) and called that because it could do an implicit conversion.

  • why does the function do implicit conversion – rooni Feb 15 '18 at 12:40
  • 1
    Why? Because it can. The language dictatces this is allowed. If you want to avoid it look up the "whole value idiom" – doctorlove Feb 15 '18 at 12:42
  • Probably so you don't have to do an explicit conversion. If you don't like that C++ allows you do this and silently converts your variables that is fair; I don't either (specifically in the case of implicit conversions between signed and unsigned types). You can probably set up your compiler to warn you when it does this. – Garrett Gutierrez Feb 15 '18 at 12:43
  • Contoversial but possibly useful: http://martin-moene.blogspot.co.uk/2012/07/light-on-whole-value.html Essentially make a struct for the types you want to aavoid the automatic casts or muddling different data types for different purposes. – doctorlove Feb 15 '18 at 13:04
1

Why this function call works?

Have a look to implicit conversions. They are automatically performed when a value is copied to a compatible type, and int and double are (12.3 and 34.3 are doubles, not floats). Then, the decimal digits are lost and only the integral part will be printed.

rooni
  • 1,036
  • 3
  • 17
  • 33
FrankS101
  • 2,112
  • 6
  • 26
  • 40
  • the OP both fixed the return type and the float/double typo. Also, there's already an answer mentioning implicit conversion. Was there something missing in the given answer that you feel your answer provides? – default Feb 15 '18 at 12:41
  • @Default I was editing the answer and didn't see the update from the OP or the other answer, simply that. – FrankS101 Feb 15 '18 at 12:43
  • 2
    @Default even if this answer would not add anything to an already given answer that is no reason for not writing it. To the contrary, there are good reasons, why there can be more than one answer – 463035818_is_not_an_ai Feb 15 '18 at 12:44