Getting error name Function should have prototype

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • darshan123
    New Member
    • Aug 2014
    • 1

    Getting error name Function should have prototype

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

    void main()
    {
    int i=1,j=2;
    float k,l=1.5;

    printf("%d %d\n",i,j);
    printf("%d",i,j );
    k=square(l);
    printf("%f\n",k );


    }
    float square(float m)
    {
    float z;
    z=m*m;
    return(z);
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You have this function:
    Code:
    float square(float m)
     {
     float z;
     z=m*m;
     return(z);
     }
    You call this function in main(). Unfortunately, the compiler hasn't seen the function yet so it thinks there is an error.

    In this case, you copy the first line of the function and follow it with a semi-colon:

    Code:
    float square(float m);
    Put this line of code above the main() function. When the compiler sees this prototype, it will allow the call even though it hasn't seen the code yet. The code may be in another file.

    This is the function prototype that s being requested.

    Comment

    • coderHead
      New Member
      • Feb 2012
      • 6

      #3
      Read this about the need for prototypes. Read mark_poc's response:

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        You are familiar with the need to declare variables before using them. Similarly, the compiler expects you to declare functions before you use them. There are three ways to satisfy this need:
        1. With a function prototype (as described above by @weaknessforcat s). This identifies the return type of the function, the number of parameters, and the types of each parameter.
        2. Define (implement) the function before you use it. The function definition acts as a declaration. This leads to an "inside-out" layout of the source file where the lowest-level functions appear first and you have to start at the bottom of the file to see the big picture.
        3. Use old-style function declarations (that identify only the return type of the function and say nothing about the parameters). The old-style declarations have been deprecated since function prototypes were introduced in the late 1980's. Don't do this.

        Comment

        Working...