tags:

views:

135

answers:

5
#include <stdio.h>

double IncomeTAX(double i)
{
    double tax;

    if(i <= 120000)        
        tax = 0;    
    else        
        tax = (i - 120000) * 15/100;    

    return tax;
}


int main(void)
{    
    double a, b;

    printf  ("Enter the annual income: ");
    scanf   ("%lf", &b);

    a = IncomeTAX(b);

    printf ("Income Tax is %.2lf\n", a);    
}

When I try to run the program in this manner it works, but when I separate the procedure, the program compiles but gives a junk value. But if I change the datatype in the procedure from 'double' to 'long' the program works even if the procedure is compiled separately.

Can anyone tell me what is the reason behind this?

+5  A: 

If by "separate the procedure" you mean "put the IncomeTAX() function in a different source code file", than perhaps you are failing to declare it before use. Put this in before main():

extern double IncomeTAX(double);

Normally this would live in IncomeTAX.h, the header file.

The reason you need this declaration is that otherwise C will default to using int; which causes problems since it can be of a different size than double.

unwind
Why `extern` ? If I understand correctly, the answer of @tvanfosson suggest the same thing without `extern`. Again I think I am missing some subtlety...
Cedric H.
funtions in C have external linkage by default unless they are specified to be static.
Prasoon Saurav
@Cedric - if it were a global variable, *extern* would be required to ensure that you don't get two instances. For a variable (without *extern*), the declaration is the definition. For a function, unless you specify the body, it can tell that the definition is just that and won't create two instances.
tvanfosson
OK, thanks for this clarification !
Cedric H.
+2  A: 

Declaration and automatic typing. If you don't declare the procedure before you use it, it assumes that it's an integer type. Putting them in the same file has the added effect of the definition serving as the declaration. If you would simply declare the procedure in the main file as returning a double that would work as well when you separate them.

FWIW, there's something poetic about an IncomeTax function returning double...

#include <stdio.h>

double IncomeTAX(double i);

int main(void)
{    
    double a, b;

    printf  ("Enter the annual income: ");
    scanf   ("%lf", &b);

    a = IncomeTAX(b);

    printf ("Income Tax is %.2lf\n", a);    
}
tvanfosson
A: 

Where you have 15/100 is an error. These two numbers are integers, and therefore return an integer, 0. Replace with 0.15 / 0.15f.

Alexander Rafferty
but `i` is a double!
pmg
No. (i - 120000) is a double - multiplied by 15 it is a double, and divided by 100 is still a double.
sje397
+3  A: 

In the absence of a prototype, the compiler tries to be helpful and guess the right types of return value and arguments to functions.

Sometimes it guesses wrong (especially when types are not int).

Solution: always use prototypes to your functions and create header files if you're going to separate functions.

pmg
so basically the compiler here sees the call to IncomeTax with a float instead of a double and assumes the signature parameter's type is float? Interesting :)
lorenzog
No, it assumes (`C89` compiler) both the argument and return value type are `int`, and generates code as if they were. The real function "fetches" a `double` from the stack (or wherever the compiler decided to put it) and "deposits" another double as the answer.
pmg
A: 

I'm assuming that by "separate the procedute", you mean move the procedure in a separate file.

In this case, the problem is likely because you aren't declaring your procedure before you use it. If you don't declare your procedure, the compiler will assume that all arguments are of type int.

What you need to do is declare your procedure as follows, somewhere before you call it.

double IncomeTAX(double i);

The usual convention is to add it to a header file; for instance, if your IncomeTAX() procedure is defined in tax.c you would declare it in tax.h, and then in the file that includes main(), you would add:

#include "tax.h"
Brian Campbell