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
Solved

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

Posted on 2004-08-13
6
2,624 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

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

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 conditional statements 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.

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