prototye error in user defined functions.

mayhbp's Avatar, Join Date: Jun 2007
Light Poster
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's Avatar, Join Date: Dec 2006
Team Leader
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's Avatar, Join Date: Jun 2007
Light Poster
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);
}

Last edited by shabbir; 26Jun2007 at 09:10.. Reason: Code block
DaWei's Avatar, Join Date: Dec 2006
Team Leader
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's Avatar, Join Date: Jun 2007
Light Poster
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's Avatar, Join Date: Jul 2004
Go4Expert Founder
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's Avatar, Join Date: Jun 2007
Light Poster
thans shabbir,that really helped.now i can proceed in my quest of learning c.

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's Avatar, Join Date: Dec 2006
Team Leader
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's Avatar, Join Date: Jun 2007
Light Poster
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's Avatar, Join Date: Jun 2007
Light Poster
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