0

In my program, I have this requirement where I have to pass a float value back to the main function.

The code for the first approach is:

#include <stdio.h>
void main()
{
    int a,b;
    a=1;
    b=2;
    division(a,b);
}

int division(int a, int b)
{
    float res;
    res=((float) a)/b;
    printf("%f",res);
}

The code for the second approach (where I pass the float value back to the main function) is this:

#include <stdio.h>
void main()
{
    int a,b;
    a=1;
    b=2;
    float res;
    res=division(a,b);
    printf("%f",res);

}

int division(int a, int b)
{
    float res;
    res=((float) a)/b;
    return res;
}

For the first approach, the output is 0.5, as expected. However, when I pass the result float (as shown in the next program), I am getting an output as 0. Why does this happen ? Is there any work-around for this ? I am relatively new to C (trying something apart from Java), so I am not very familiar with it's rules. When I modified int division to include a printf statement for variable res, like

int division(int a, int b)
{
    float res;
    res=((float) a)/b;
    printf("Variable before passing %f ",res);
    return res;
}

I am getting the output as Variable before passing 0.500000 0.000000, which lead me to believe that the problem is with the passing of parameters back to the main function.

theboss12k
  • 125
  • 8
  • 6
    What is the declared return type of `division`? – Some programmer dude Aug 15 '22 at 07:50
  • 1
    And why don't you make the `division` arguments `float`? – Some programmer dude Aug 15 '22 at 07:50
  • 3
    On a different note: Unless you're targeting a minor embedded system where each byte matter, there's really no need to use `float` these days. Use `double` for all your floating point calculations and variables. – Some programmer dude Aug 15 '22 at 07:52
  • 1
    might aswell change the first approach `division()` function return value to void, since you do not return anything. – mikyll98 Aug 15 '22 at 07:52
  • @Someprogrammerdude, making the `division` arguments float results in this error: `[Error] conflicting types for 'division' ` – theboss12k Aug 15 '22 at 07:52
  • 1
    `"However, when I pass the result...` In computer science, you 'pass' arguments to functions that 'return' values... The function does not 'pass' a return value to the caller. – Fe2O3 Aug 15 '22 at 07:53
  • 1
    And you get that error because you forgot to add a *forward declaration* of the function. C really requires you to declare functions before you can call them. Unfortunately many compilers are rather bad in enforcing it. Enable more warnings when building and the compiler should at least warn you about it. Enabling more warnings (that you treat as errors) is generally a good idea. – Some programmer dude Aug 15 '22 at 07:54
  • @Someprogrammerdude, Ok, forward declaration seems to have solved the problem. Thank you for the help. Should I put that as an answer, or shall I delete the question ? – theboss12k Aug 15 '22 at 07:55
  • 1
    @mikyll98, Thanks for clarifying. Adding forward declaration along with argument modification seems to have done the job – theboss12k Aug 15 '22 at 07:58
  • 1
    @Fe2O3, Yup that was my mistake in terminologies. Thank you for correcting it. – theboss12k Aug 15 '22 at 07:59
  • Related read [here](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings). – n. m. could be an AI Aug 15 '22 at 08:06

3 Answers3

4

There are actually two problems with the code and the division function:

  1. It's declared to return an int instead of a float. This leads to truncation in the conversion from float to int.

  2. The second problem is that you need to declare the function before you call it. C used to have implicit declarations where calling a previously undeclared function would make the compiler assume the arguments and return type of the function. One of the assumption would be that the function returned an int. And if you correct the division function you will then have it not match the implicit declaration by the compiler.

To solve both problems, I suggest you move the division function to be defined (and declared) before the main function, and using the correct types.

I also suggest you use double instead of float as types, and that you declare the arguments as such as well.

So perhaps something like this:

#include <stdio.h>

double division(double a, double b)
{
    // Exercise for reader: To protect against division by zero
    return a / b;
}

int main(void)
{
    int a = 1;
    int b = 2;
    printf("%f\n", division(a, b));
}

Note that I changed the return type of main from void to int. The main function is specified to always return an int. However you don't need an explicit return statement in the main function, if you leave it out the compiler will implicitly add a return 0; at the end.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

Your second approach is almost correct:

int division(int a, int b)
{
    float res;
    res=((float) a)/b;
    return res;
}

Here, division() is declared to return an int value, so the float you calculate is converted to int before being returned. You need to change it into:

float division(int a, int b)
...
Luis Colorado
  • 10,974
  • 1
  • 16
  • 31
-2

Well, thanks to the help of @Some programmer dude and @mikyll98, I was able to fix the code. The problem was that I had not done forward declaration and had put a wrong return type of int instead of float.

Corrected code:

#include <stdio.h>
float division(int a, int b);
void main()
{
    int a,b;
    a=1;
    b=2;
    float res;
    res=division(a,b);
    printf("%f",res);

}

float division(int a, int b)
{
    float res;
    res=((float) a)/b;
    printf("Variable before passing %f ",res);
    return res;
}
theboss12k
  • 125
  • 8
  • Had you read the compilation warnings, you would have worked this out for yourself. The compiler is your best friend when it comes to syntax. And code without syntax is without semantics... – Fe2O3 Aug 15 '22 at 08:07
  • 3
    why don't you accept people's answer instead of including your own? They have basically the same code but with a better explanation. – jeekiii Aug 15 '22 at 08:23
  • 1
    theboss12k, Aside: In C , better to define and initialize in the same line: `float res; res=division(a,b);` --> `float res = division(a,b);`, `float res; res=((float) a)/b;` --> `float res = ((float) a)/b;`. – chux - Reinstate Monica Aug 15 '22 at 10:22
  • theboss12k Why code with `float` here as `double` 1) does not lose precision that `float` might in `(float) a` and `res` is converted to `double` in `printf("Variable before passing %f ",res);` anyways? – chux - Reinstate Monica Aug 15 '22 at 10:25
  • @jeekiii, Please check the time of posting of the answers. The answer by Some Programmer dude had not been posted when I posted my answer. I have marked that answer as the correct one. – theboss12k Aug 15 '22 at 15:59
  • 1
    @chux-ReinstateMonica, Thank you for your input. I will keep that in mind next time. Also, I will code using double from now. Thank you once again – theboss12k Aug 15 '22 at 16:01
  • @Fe2O3, I tried to figure it out on my own using the compiler warnings, but it just didn't seem to work out. Anyways, thank you so much for your help – theboss12k Aug 15 '22 at 16:02