program. main() is known as a self-prototyping function because no other functions
call main() before it appears in the code.
The following program does not work correctly because the float return type is not prototyped
correctly. Remember, C assumes that an int is returned (even if you return a different data type)
unless you override the return type in the prototype.
Click here to view code image
#include <stdio.h>
compNet(float atomWeight, float factor);
main()
{
float atomWeight, factor, netWeight;
printf("What is the atomic weight? ");
scanf(" %f", &atomWeight);
printf("What is the factor? ");
scanf(" %f", &factor);
netWeight = compNet(atomWeight, factor);
printf("The net weight is %.4f\n", netWeight);
return 0;
}
/************************************************************/
compNet(float atomWeight, float factor)
{
float netWeight;
netWeight = (atomWeight – factor) * .8;
return(netWeight);
}
This shows the incorrect output:
Click here to view code image
What is the atomic weight? .0125
What is the factor? .98
The net weight is 0.0000
To fix the problem, you have to change the prototype to this:
Click here to view code image
float compNet(float atomWeight, float factor);
You also have to change the compNet()’s definition line (its first line) to match the prototype:
Click here to view code image
float compNet(float atomWeight, float factor)
Wrapping Things Up
Never pass or return a global variable if you use one. Global variables don’t have to be passed. Also,
the parameter lists in the calling function, receiving function, and prototype should match in both