
Hello Varun, really nice question.
A lot of beginners run into this when they start working with functions in C, so youāre definitely on the right track noticing it.
Hereās the simple idea:
If you write a function like this:
int getProduct(float a, float b)
C expects that function to return an int, no matter what you actually calculate inside it. So even if a * b produces a floating-point value, C will convert it to an integer when returning it. Thatās why the decimal part disappears.
If you want the full, precise result, you need the function to return a floating-point type:
double getProduct(double a, double b) {
return a * b;
}
This keeps the decimal values and avoids losing accuracy.
And one more thing students often miss:
You have to actually use the returned value, otherwise it's wasted:
double result = getProduct(3.5, 4.6);
printf("Product: %.2f\n", result);
If anything about return types still feels unclear, feel free to ask ā Iām happy to help you understand it fully.
Our Experts
Sudip BhandariHead of Growth/Marketing
Apekchhya ShresthaSenior Product Manager
Kelish RaiTechnical Content Writer
Abhilekh GautamSystem Engineer
Palistha SinghTechnical Content Writer
Sarthak BaralSenior Content Editor
Saujanya Poudel
Abhay Jajodia
Nisha SharmaTechnical Content Writer
Udayan ShakyaTechnical Content Writer
