Return type in C | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 26

Return type in C

for example a function return type is int but can still output floats. Considering this code. Does it mean the return type doesnt affects outputs of the code? Can someone please shed some lights. int sum (int, int); int main(){ int x = 5; int y = 10; float result =sum(x + y); Printf("%d + %d = %.2f", x,y, result); } int sum(int x, int y){ return x + y; }

18th Sep 2019, 1:49 AM
BlackRose Mike
BlackRose Mike - avatar
4 Answers
+ 16
Fuzzy Squid Then how do we use it or make it do something as you said
18th Sep 2019, 1:54 AM
BlackRose Mike
BlackRose Mike - avatar
+ 16
Maninder $ingh i see, i was expecting it to throw an error or a warning but it didnt.
18th Sep 2019, 2:37 AM
BlackRose Mike
BlackRose Mike - avatar
+ 7
BlackRose Mike this is your answer man. Actually if you look deep in your code.your function sum actually return a int value,but in main function you are storing your int value returning from function in result variable which is float type that's why result variable converting your int value to float value. But actually sum function always returning int value.
18th Sep 2019, 2:32 AM
Maninder $ingh
Maninder $ingh - avatar
+ 7
BlackRose Mike try this code actually your code have many errors. int sum(int x, int y){ return x + y; } int main(){ int x = 5; int y = 10; float result = sum(x,y); printf("%d + %d = %.2f", x,y, result); }
18th Sep 2019, 2:46 AM
Maninder $ingh
Maninder $ingh - avatar