Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

To understand function()

Status
Not open for further replies.

kreop

Technical User
Oct 9, 2005
14
JP
hi...Sorry,this is a basic question about function. I tried to find the solution from net but failed. Here is a sample code of C

#include<stdio.h>
int mult(int); //prototype
main()
{
int a;
printf(" Enter the Integer!");
scanf("%d",&a);
printf("2x%d is %d",a,mult(a)); //call function
}
int mult(int x) //function definition
{
return x*2;
}

What is the relation between prototype and function definition in term of address in memory. int mult(int)/prototype-means that user requires 4 byte space in memory with a specific address and how about int mult(int x)/function defination??From my understanding, function definition also requires 4 byte space in memory but at a diferent address with prototype.

What was confusing me is, why prototype is needed in the code because the calculation of function is done in function definition. Or what is the function of prototype and does it requires space in memory.

I am a newer in programming and may be my understanding about the whole things above is wrong. Please correct my understanding if I am wrong...

Thanks.

 
The prototype doesn't generate any code. It just tells the compiler some information about the function (parameters, return value) in case it encounters a call to the function before it's seen the function definition.

So, the prototype doesn't generate any code or require any memory. The function definition does generate code and take memory.

Hope this helps.
 
C has default prototypes which are of type int. If your function returned a double and you did not declare a prototype, then you would get a compilation warning/error that the function was being redefined. Also, when you run it, it will treat it as an int function instead of a double and you might get the wrong result.

It is like those cop movies where when arresting someone, they recite MIRANDA "...if you don't have one, one will be provided for you". Some people call C prototypes MIRANDA prototypes.
 
and may be my understanding about the whole things above is wrong"

I think you were right about that. As our friends indicated b4, Prototyping is not for generating executable code, it is to make your compiler smarter and reduce the chances that you will shoot yourself in the foot by catching what would have been run time error early on in the compiling stage.

For example, if you try to compile this code it will generate a <Compile-Time> error f:\C\badpointer\badpointer.cpp(14): error C2664: 'Mult' : cannot convert parameter 1 from 'char *' to 'int'

#include <stdio.h>
int Mult(int);
int Mult(int x)
{
return x*2;
}
int main()
{
char *str = "Hi There, Can you multiply me by 2";
int x = Mult(str);
}

Again it is Compile-Time, not a run time error. The compile “Knows” for sure that if this code was to be executed it will generate a run-time error so it refused to compile it.

How did the compiler know? Because of the prototype.


In short, prototypes are used in the compile time only to verify the integrity of the code to be compiled. Prototypes do not generate any executable code.


Walid Magd

The primary challenge of every software development team is to engineer the illusion of simplicity in the face of essential complexity.
-Grady Booch
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top