1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

prototye error in user defined functions.

Discussion in 'C' started by mayhbp, Jun 25, 2007.

  1. mayhbp

    mayhbp New Member

    Joined:
    Jun 25, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    hello friends,
    i am a newbie in C programming its only my 10th day. i have just reached functions.
    if i write any user defined function the compiler give the following error.

    function 'sumprod' must have a prototype..

    until now ie with
    loops and repetition control insts i had no errors.but now i am lost..
    could u please guide me as to what i should do.

    thanks.
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    Your information is scanty, so I'll hazard a guess, which is a dangerous thing to do.

    When your compiler sees a call to a function, it has to generate machine code to call that function and it has to place the arguments in a place (which has been agreed upon before) where the function may find them.

    Obviously, it can't do that without knowing the number and form of the arguments. This means that the function's definition, or a description of it, must occur before the compiler encounters the call. The compiler can be appeased by writing the function before you write the part that calls it, or it can be appeased if you provide a pattern (prototype) before you call it.

    If you fail to meet any of these requirements in C, the compiler will assume that the function takes an int argument and returns an int. This presumption can lead to problems if the presumption is not true. C++ will not presume anything; it will merely run off into the weeds and puke on its shoes.

    You are the architect and designer; C/C++ is just the hourly paid slave. Do your job.
     
  3. mayhbp

    mayhbp New Member

    Joined:
    Jun 25, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    well this is the source code

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    	int a=10,b=20,c=30,s;
    	clrscr();
    	s=sumprod(a,b,c);
    	printf("\n%d",s);
    	getch();
    }
     sumprod(int x,int y,int z)
    {
    	int ss;
    	ss=x+y+z;
    	printf("\n%d",ss);
    	return(ss);
    }
     
    Last edited by a moderator: Jun 26, 2007
  4. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    I won't read that until you become a polite member of this society. That means reading the "Before you post a query" thread and learning to use code tags.
     
  5. mayhbp

    mayhbp New Member

    Joined:
    Jun 25, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0

    sorry mate,just new in town thanks for showing me around..
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    [COMMENT]I have added the code blocks[/COMMENT]

    There are 2 ways your problem can be solved.

    1. Define sumprod(int x,int y,int z) above the main.
    2. Have the function declaration (prototype) above the main and then have the function definition where you are having it right now.
     
  7. mayhbp

    mayhbp New Member

    Joined:
    Jun 25, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    thans shabbir,that really helped.now i can proceed in my quest of learning c.:D

    i got the 1st senario,but could not get the second one.
    could you please tell me what the second one means.i am really new to programing.


    thanks

    ps.how do i add code tags.
     
  8. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    Simple. You put [/code] below the code and
    Code:
     above.  You may wonder why I detailed those ***-backwards, but it was so part of my response wouldn't be rendered as code.
     
  9. mayhbp

    mayhbp New Member

    Joined:
    Jun 25, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
     
  10. mayhbp

    mayhbp New Member

    Joined:
    Jun 25, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0

    ok ok now i get it,
    1. is the ANSI style
    2. the K&R style
     
  11. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    Nope. They are the "how it works" style. It isn't a style, it's a requirement.
     

Share This Page