Solved

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

Posted on 2004-08-13
6
2,628 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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!

 
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

Independent Software Vendors: 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

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

689 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