tags:

views:

179

answers:

3
//include all necessary header files

Void Main()
{
    float a;
    b=square(a);
}
int square(float a )
{
    int y;
    y=a*a;
    return(a);
}

When i makes argument int type then it is working.

Please do not define prototype or write function over main.

Tell Me logic why this gives error.

+2  A: 

Once you've dealt with the problems cited in the comments, you can get to the problem you originally knew about. The problem is fairly simple: in the absence of a prototype to specify the types of arguments to functions, all arguments undergo a set of default promotions before being passed to the function. The smaller integer types get promoted to int, and float gets promoted to double. The compiler then treats that function name (roughly) as if it had a prototype with those promoted argument types specified.

In your case, it then gets to the definition of the function, and finds that it specifies an argument of type float, which doesn't match the promoted type (double) that it has already decided that function must have. It reacts to that mismatch by giving you an error saying that square, which was originally (implicitly) declared to take an argument of type double has been re-declared to take an argument of type float, which isn't allowed.

Edit: The integer promotions are covered in §6.3.1.1 and the promotions applied when a function lacks a prototype (integer promotions + float->double) are covered in §6.5.2.2/6 of the C99 standard. Most other reasonably modern books on C will normally expect you to use prototypes for functions, so I'd guess most don't say much about what happens without them.

Jerry Coffin
+1: "...all arguments undergo a set of default promotions before being passed to the function." I didn't knew that, how can I get more info on this ???
Manav MN
Hi Jerry CoffinI am very much happy with your explanation and i wrote one thing wrong in my code return(y);should be there .ThanksRahul
Rahul Bhargava
A: 

Since your question is so unclear, I guess I'll just go over everything that's wrong:

Void Main()

This is not a valid declaration of main, which must be declared as returning an int.

{
    float a;
    b=square(a);
}

No b variable is declared, and you're invoking square on an uninitialized variable, so you're passing garbage to your function.

Also note that you haven't given a function prototype (a.k.a a function declaration) for your square function. I'm guessing that this is what your compiler is complaining about; without the declaration, it will make assumptions about what your function returns and about what types of arguments it takes (assumptions that in this case will be wrong).

int square(float a )

It doesn't make sense for square to take a floating point argument but to return an integer type. If you called square(2.5) (and assuming your function worked), instead of returning 6.25, it would return 6.

{
    int y;
    y=a*a;
    return(a);
}

Here you're returning the argument you were passed in (rounded toward 0 due to the conversion to int), not the squared result, so your function doesn't actually do what it's supposed to do.

jamesdlin
Note that in C, a prototype is *different* from a function declaration. A function declaration looks like: `int func();`, and says nothing about parameter types. A prototype looks like: `int func(float);`, and tells the number and type of parameters for that function. Just to confuse the issue further, a C++ function declaration is essentially similar to a C function prototype.
Jerry Coffin
A: 

I hope you are using Turbo C compiler. Because it is the only compiler i knew which accepts "void main()" like things. Better start using gcc compiler. Its found in almost every linux distribution. I prefer Ubuntu for who are new to Linux. you can download or request free cd of it from http://www.ubuntu.com/.

srikanthM