?
Solved

pls help Error "function 'rec' should have a prototype"

Posted on 2004-08-13
6
Medium Priority
?
2,641 Views
Last Modified: 2008-01-09

hi experts,
please help me to fix this problem. i will be really thankful to you.
Every time when I write some program using functions i got this error message."function 'XXX' should have a prototype."
below is one of the program that i wrote using functions, and got the same error message,
"function 'rec' should have a prototype."
Please considor i am new in 'C' so please explain in a simple way. thanks


#include<stdio.h>
#include<conio.h>
void mian()
{
int a,fact;
printf ("\n Enter any number");
scanf ("%d",&a);
fact=rec(a);
printf ("Factorial value = %d",fact);
getch();
}
rec(int x)
{
int f;
if(x==1)
return(1);
else
f=x*rec(x-1);
return(f);
}
0
Comment
Question by:deepthiji
6 Comments
 
LVL 5

Assisted Solution

by:millsoft
millsoft earned 800 total points
ID: 11794365
#include<stdio.h>
#include<conio.h>
rec(int x)
{
int f;
if(x==1)
return(1);
else
f=x*rec(x-1);
return(f);
}

void mian()
{
int a,fact;
printf ("\n Enter any number");
scanf ("%d",&a);
fact=rec(a);
printf ("Factorial value = %d",fact);
getch();
}
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 600 total points
ID: 11794370
Just add the prototype:

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

int rec(int x); /* prototype for ''rec()" */

void mian()
{
int a,fact;
printf ("\n Enter any number");
scanf ("%d",&a);
fact=rec(a);
printf ("Factorial value = %d",fact);
getch();
}
int rec(int x)
{
int f;
if(x==1)
return(1);
else
f=x*rec(x-1);
return(f);
}
0
 
LVL 5

Expert Comment

by:millsoft
ID: 11794382
By putting the "called" function rec before main, then when the compiler sees the function call in main (fact=rec(a);), it knows that rec is a function, and the sort of parameters & return value the function has.

This pre-declaration of a function's parameters & return type is called a "prototype".

Prototypes can be specified in two ways:
1. Simply defining the function before it is called (by putting rec before it's use in the C file)
or
2. including a prototype line before the use of the function: e.g. int rec(int x);
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 5

Expert Comment

by:millsoft
ID: 11794394
jkr's example uses method 2, my code uses method 1.

Also, note that the main function is misspelled in your example, so it will compile, but not link.
0
 
LVL 9

Accepted Solution

by:
ankuratvb earned 600 total points
ID: 11794463
In addition to whats been said,read up on function prototyping:

http://www.cs.cf.ac.uk/Dave/C/node8.html#SECTION00830000000000000000
0
 
LVL 3

Author Comment

by:deepthiji
ID: 11794509
Thanks a lot for the quick replies
I will split the points and thanks once again.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question