Why Can't a Function Go After Main

Why can't a function go after Main

You can, but you have to declare it beforehand:

void myFunction(); // declaration

int main()
{
myFunction();
}

void myFunction(){} // definition

Note that a function needs a return type. If the function does not return anything, that type must be void.

Why there are function definitions after the main()?

There's absolutely nothing wrong with what Stallman did here.

The C language permits the forward declaration of a function that will be defined afterwards.

This has many advantages, and should not be considered as bad behavior, but rather very good behavior.

Advantages (not exhaustive):

- give the programmer a vision of the API exposed by the C code in a quick look, without having to look at all the code

- permits the use of header files, where you declare a function that will be defined later on in the compilation process. So that you don't have to define your function every time you use it..

In the case of this ls implementation, he simply pre-declared the functions that he'll use in the main(), but if you look carefully, the main function is the first one to appear.
This is most probably for the sake of readability, so that you don't have to scroll all the way down to reach the entry point of the program.

Note that the vocabulary is important here:

- function declaration means: just tells the compiler that, somewhere in your code, a function with the same name will be defined.

- function definition : the actual function implementation

int my_function( char *text); // function declaration, no implementation
int main( int argc, char **argv)
{
return my_function(argv[0]); // use of the declared function
}

// actual function definition / implementation
int my_function( char *text )
{
printf("%s\n", text);
}

Edit: after looking more carefully to the code, you can see that Stallman didn't forward-declare all his functions. He has also a rather strange manner of defining functions. I attribute this to the oldness of the code, which is dated of 1985, when the C compiler was not as well defined as today.
It must have permitted this kind of function usage before being declared or defined.

Last but not least, the recent version of ls source code can be found here: http://coreutils.sourcearchive.com/documentation/7.4/ls_8c-source.html ,

with much more C99-compliant coding than the '85 (Back-to-the-Future) version.

Why can't we define function inside the main function?

m is defined inside of main. In standard C, that's not allowed (you can't define a function within another function).

Some compilers (e.g. gcc) allow it as an extension. But then the function is local, i.e. m only exists within main and can't be seen from the outside. Similarly, variables defined within a function are local to that function and can't be seen from the outside.

Your void m(); declaration at the top claims that a (global) function called m exists, but it doesn't. That's why you get the linker error.

Does int main() need a declaration on C++?

A definition of a function is also a declaration of a function.

The purpose of a declaring a function is to make it known to the compiler. Declaring a function without defining it allows a function to be used in places where it is inconvenient to define it. For example:

  • If a function is used in a source file (A) other than the one it is defined in (B), we need to declare it in A (usually via a header that A includes, such as B.h).
  • If two or more functions may call each other, then we cannot define all those functions before the others—one of them has to be first. So declarations can be provided first, with definitions coming afterward.
  • Many people prefer to put “higher level” routines earlier in a source file and subroutines later. Since those “higher level” routines call various subroutines, the subroutines must be declared earlier.

In C++, a user program never calls main, so it never needs a declaration before the definition. (Note that you could provide one if you wished. There is nothing special about a declaration of main in this regard.) In C, a program can call main. In that case, it does require that a declaration be visible before the call.

Note that main does need to be known to the code that calls it. This is special code in what is typically called the C++ runtime startup code. The linker includes that code for you automatically when you are linking a C++ program with the appropriate linker options. Whatever language that code is written in, it has whatever declaration of main it needs in order to call it properly.

c++ Class declaration after main?

You can't do it how you've written it. The compiler has to be able to see the definition of the class before it can use it. You need to put your class before your main function, or preferably in a separate header file which you include.

What are the problems that are mitigated by not allowing nested function declarations in Go?

I think there are 3 reasons why this obvious feature isn't allowed

  1. It would complicate the compiler slightly. At the moment the compiler knows all functions are at the top level.
  2. It would make a new class of programmer error - you could refactor something and accidentally nest some functions.
  3. Having a different syntax for functions and closures is a good thing. Making a closure is potentially more expensive than making a function so you should know you are doing it.

Those are just my opinions though - I haven't seen an official pronouncement from the language designers.

Define a function before main?

How and where to prototype and define a function in C :

  1. Your function is used only in a specific .c file :
    Define it static in the .c file. The function will only be visible and compiled for this file.

  2. Your function is used in multiple .c files :
    Choose an appropriate c file to host your definition (All foo related functions in a foo.c file for example), and have a related header file to have all non-static (think public) functions prototyped. The function will be compiled only once, but visible to any file that includes the header files. Everything will be put together at link time. Possible improvement : always make the related header file, the first one included in its c file, this way, you will be sure that any file can include it safely without the need of other includes to make it work, reference : Large Scale C++ projects (Most of the rules apply to C too).

  3. Your function is inlinable (are you sure it is ?) :
    Define the function static inline in an appropriate header file. The compiler should replace any call to your function by the definition if it is possible (think macro-like).

The notion of before-after another function (your main function) in c is only a matter of style. Either you do :

static int foo(int foo) 
{
// code
return 1;
}

int main(void)
{
foo(1);
return 0;
}

Or

static int foo(int foo);

int main(void)
{
foo(1);
return 0;
}

static int foo(int foo)
{
// code
return 1;
}

will result in the same program. The second way is prefered by programmers because you don`t have to reorganize or declare new prototypes every time you declare a new function that use the other ones. Plus you get a nice list of every functions declared in your file. It makes life easier in the long run for you and your team.



Related Topics



Leave a reply



Submit