Solved

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

Posted on 2004-08-13
6
2,619 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
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 150 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
[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

 
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 150 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
My eclipse editor won't start ? 4 144
Why is compiler in oracle server ? 9 45
What is sub-make ? 2 39
nested if statement in excel help 4 12
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

920 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

14 Experts available now in Live!

Get 1:1 Help Now