C is all about functions. Right? Well, it’s structured around functions. A program written in C always starts with a function called main and then calls various functions along the way. Some of the functions are part of the standard library and some are user defined. I wanted to know if you could write a function within a function. It happens all the time in other languages. Why not C?
I started with a bare bones main function.
int main()
{
// return some value
return 0;
} // end main
That code works. It compiles. It runs. But nothing happens.
I then added a function fn() inside this main function.
int main()
{
// Prototype declaration.
int fn();
// return some value
return 0;
int fn()
{
return 0;
}
} // end main
It wouldn’t compile. I got this compile error:
error C2601: 'fn' : local function definitions are illegal
End of that story. You can’t define a function inside of another function in C. Not sure why. I expect there is some sort of explanation. Must be. But for now, C is a series of functions, all separate, one after another with the focus on the main function.
No comments:
Post a Comment