Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   prototye error in user defined functions. (http://www.go4expert.com/forums/prototye-error-user-defined-functions-t4899/)

mayhbp 25Jun2007 23:21

prototye error in user defined functions.
 
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.

DaWei 25Jun2007 23:47

Re: prototye error in user defined functions.
 
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.

mayhbp 26Jun2007 00:24

Re: prototye error in user defined functions.
 
Quote:

Originally Posted by mayhbp
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.

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);
}


DaWei 26Jun2007 00:39

Re: prototye error in user defined functions.
 
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.

mayhbp 26Jun2007 00:42

Re: prototye error in user defined functions.
 
Quote:

Originally Posted by DaWei
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.


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

shabbir 26Jun2007 09:13

Re: prototye error in user defined functions.
 
Offtopic comment:
I have added the code blocks


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.

mayhbp 27Jun2007 01:20

Re: prototye error in user defined functions.
 
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.

DaWei 27Jun2007 02:04

Re: prototye error in user defined functions.
 
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.

mayhbp 27Jun2007 03:15

Re: prototye error in user defined functions.
 
Quote:

Originally Posted by DaWei
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.


got it sir,thank you.
ill remember it henceforth.

mayhbp 27Jun2007 03:18

Re: prototye error in user defined functions.
 
Quote:

Originally Posted by shabbir
Offtopic comment:
I have added the code blocks


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.


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


All times are GMT +5.5. The time now is 16:22.