wats the use of function prototype , regarding that we cannot add it to the code and the prog runs perfect ?
wats the use of function prototype , regarding that we cannot add it to the code and the prog runs perfect ?
Function prototypes exist for 2 reasons...
1) C can't compile anything it doesn't already know about. Thus it can't compile a call to a function it hasn't seen yet. Since we can't always order functions that way (eg where Foo calls Bar and Bar also calls Foo) Prototypes provide a "forward reference" to allow the the compiler to work with out of order definitions.
2) In program headers, which are included into other programs, prototypes allow the compiler to work with functions that exist in other pages of source code.
If you're working on a project with multiple source files, you need to write a header file and put function prototypes in there, then include the header in each source file, so that functions are aware that other functions exist. You also need to declare prototypes if you wrote other functions after the main function.
Originally Posted by The Jargon File
That is not entirely correct.
GCC, f.ex., will compile a function w/o a prototype -- a warning will be given but it will compile. What you will lose when you don't provide a function prototype is type checking when making a function call. The below code will compile, but run and crash because I used bad variable names and made a typo. No warnings will be given about incorrect argument type. If you uncomment the prototype, you'll get a warning.
Code:#include <stdio.h> /* int msh_strlen(const char* string); */ int main(void) { int s; char* S = "hello, world!"; s = msh_strlen(s); printf("??? s = %d\n", s); return 0; } int msh_strlen(const char* string) { int i = 0; while (*string++) { ++i; } return i; }
Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.
Othwise known as a "Forward declaration"; sure there are cases where they are not required, in which case I agree that they serve no purpose other than being documentary. However there are also other cases where a C program will not compile without them.
FYI: Some other languages such as 'D' do not require function prototypes.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
IMO... that's a weakness of C prior to C99, though it is proper behaviour since it is um, C.Originally Posted by CommonTater
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
GCC gives a warning, which everyone is free to ignore at their own peril.
I would say that this is proper behavior, as I couldn't find anything in the standard that would mandate the existence of a function prototype (declaration), only that it can exist, and that it should agree with function definition.
Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.
To be accurate, C99 does not require that a function's prototype be in scope before the function is called, but I believe that it does require that a function declaration that serves as a prototype, or the function definition, be in scope before the function is called.Originally Posted by msh
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
thnx
thats what i mean we dont have function prototype ...
[code]
#include <stdio.h>
#include <conio.h>
int enter (void)
{
int x;
printf(" enter an integer:");
scanf("%d",&x);
return x;
}
int cube (int z)
{
return (z*z*z);
}
int main()
{
clrscr;
int y,f;
y = enter();
f=cube(y);
printf("after cube:%d",f);
while(!kbhit());
return 0;
}
[\code]
so u guys say that its wrong and i shouldnot do this again ?
explain plz
Here, let me make this easy:
Functions are tools. The return type tells what you get from the function. The function name is the name of the tool. The arguments are what the tool needs to work right.
A function prototype is a description of the tool, telling you everything I mentioned in the paragraph above this, so that when you have the tool available, you know how to use it.
A function definition is the actual details of how the tool works. Think of it as a detailed diagram showing everything about the tool itself.
A function call is you using the tools.
You don't need a short description of the tool (function prototype) if you have a detailed schematic of it (function definition).
If you move the function definition to the top, you can throw out the description of the tool, since you'll have the full tool diagram available:Code:void foo( void ); /* prototype - description of tool */ int main( void ) { foo( ); /* function call - using tool */ return 0; } void foo( void ) /* function definition - schematic of tool */ { printf( "hello world\n" ); }See?Code:void foo( void ) /* function definition - schematic of tool */ { printf( "hello world\n" ); } int main( void ) { foo( ); /* function call - using tool */ return 0; }
Quzah.
Hope is the first step on the road to disappointment.
ya thnx