Solved

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

Posted on 2004-08-13
6
2,610 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 200 total points
Comment Utility
#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 150 total points
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 5

Expert Comment

by:millsoft
Comment Utility
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 150 total points
Comment Utility
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
Comment Utility
Thanks a lot for the quick replies
I will split the points and thanks once again.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

771 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now