0

When I don't declare a function protype and have a single function, it runs. However, when I have more than one function than it gives me the error:

type mismatch in redeclaration of "function-name"

#include <conio.h>
#include <stdio.h>

float area1(float, float);
float area (float, float);

void main()
{
   float x;
   float y;
   float a;
   float z;
   clrscr();
   x = 5.0;
   y = 5.0;
   z = area(x, y);
   a = area1(x, y);
   printf("%f", z);
   printf("%f", a);
   getch();
}

void area(float a, float b)
{
   int c;
   c = 0.5 * a * b;
   return c;
}

void area1(float a, float b)
{
   int c;
   c = 0.5 * a * b;
   return c;
}
Joseph Quinsey
  • 9,553
  • 10
  • 54
  • 77
StaticVariable
  • 5,253
  • 4
  • 23
  • 45
  • 8
    Pick up a good book. – Kerrek SB Jan 25 '12 at 04:36
  • where have you defined the function? it would be better if you could post the relevant code. – Sriram Jan 25 '12 at 04:37
  • i just called the function ..it runs but when i have more than one function than it gives me error? – StaticVariable Jan 25 '12 at 04:39
  • redeclaration = more than one declaration of the same function – Aram Kocharyan Jan 25 '12 at 04:40
  • 1
    @Lokesh The C bible, C Programming Language (2nd Edition) [Paperback] Brian W. Kernighan (Author), Dennis M. Ritchie (Author) – Adrian Cornish Jan 25 '12 at 04:41
  • @AramKocharyan but i don't declared the same function again? – StaticVariable Jan 25 '12 at 04:44
  • Probably a good idea to post your code then, if it's not too long – Aram Kocharyan Jan 25 '12 at 04:45
  • #include #include float area1(float,float); float area(float,float); void main() { float x; float y; float a; float z; clrscr(); x=5.0; y=5.0; z= area(x,y); a= area1(x,y); printf("%f",z); getch(); } void area(float a,float b) { int c; c=0.5*a*b; return c; } void area1(float a,float b) { int c; c=0.5*a*b; return c; }//when i don't declare the prototype than it shows erroe – StaticVariable Jan 25 '12 at 04:48
  • There are all sorts of things wrong with your code. The signature of the `main` method is wrong; there is no such thing as `void main()` in C, and the `` header hasn't existed for over a decade. You really need to read a book about C, rather than following whatever 20-year-old online tutorial that you're following now. – Cody Gray - on strike Jan 25 '12 at 04:58
  • 2
    @AdrianCornish I disagree, K&R 2nd edition is an old, outdated book preaching a lot of poor style and dangerous practices. It shall by no means be regarded as canon. As an example, the relevant section for this question in K&R (p.26) describing functions and prototypes, states that it is just fine for a function to skip its return statement, which is just _wrong_ and leads to undefined behavior. – Lundin Jan 25 '12 at 10:49
  • @CodyGray void main() is actually fine in C, but not for hosted systems, and this is obviously a hosted system. See [this](http://stackoverflow.com/questions/5296163/why-is-the-type-of-the-main-function-in-c-and-c-left-to-the-user-to-define/5296593#5296593). – Lundin Jan 25 '12 at 10:51
  • @Lundin I found this page because of a question I had from reading K&R. What alternative would you recommend? – Eric Kightley Aug 26 '13 at 14:52

4 Answers4

3

You normally want to prototype a function before use. The number of functions is irrelevant, but the order matters. There are two typical ways of doing things:

  1. Write the functions in basically reverse order, with the lowest level functions at the beginning of the file, and higher level functions (that use the lower level ones) following.
  2. Use separate prototypes (typically in a header) preceding the implementations of the functions.
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
2

you should Write the functions in basically reverse order, with the lowest level functions at the beginning of the file, and higher level functions (that use the lower level ones) following.and Use separate prototypes (typically in a header) preceding the implementations of the functions.

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
2

There are several different rules involved, but in general:

  1. If the compiler sees the prototype before the fist call to a function - it knows what to do.

  2. If the compiler sees the function itself before the first call - it knows what to do.

  3. If neither of the first 2 are true, the compiler invents a prototype based on how you called the function. If the subsequent calls don't match the "invented" prototype - you get the error you quoted. You may also get an error, instead of implicit prototyping, if your compiler is strict.

which book should i folow

I would suggest starting with K&R. There are many other (and more up to date) books, but K&R is the first to have when you're dealing with C, in my humble opinion.

littleadv
  • 20,100
  • 2
  • 36
  • 50
1

I don't see this pointed out anywhere so i guess I'll do it: your prototype declares both area and area1 functions as returning floats and taking 2 float parameters but you implement them as returning voids AND you have a return statement inside them which should pretty much make the compiler yell at you.

Secondly you try to give a int variable a float value which is pretty much undefined behaviour, so while the compiler could let that pass, it's not something you want either. So as wiser folks have suggested , pick up a good C book.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
omu_negru
  • 4,642
  • 4
  • 27
  • 38