0

In C and C++ we need to declare a function before its usage, if its definition comes after where it is called. (Well, there is also the "implicit declaration" rule in C, but it is rarely used.)

However, in more "modern" languages functions defined later in the same module are implicitly "declared" (or has the same effect as being declared). For instance, rust, python, etc.

While implicit declarations are more convenient, they seem to introduce some ambiguity when an undeclared symbol is seen. One must have a careful procedure to handle this. If the language is interpreted like python or javascript, this is not really an issue. But for compiled language, surely we must design it so that it is not ambiguous.

What are other advantages and disadvantages of explicit declaration?

3
  • "they seem to introduce some ambiguity" - ambiguity for whom?
    – Steve
    Jun 18, 2023 at 7:04
  • @Steve To clarify, the parser will know a symbol is a function for sure, if it is explicitly declared, so we get extra information. Therefore, there must exist some language design which only works with explicit declarations. Of course, you do not see it in practice because the languages are designed to avoid the ambiguity.
    – Ma Joad
    Jun 18, 2023 at 7:10
  • 1
    The parser will also know it is a function for sure if it parses all function headers before parsing any contents. The design of C is tailored to being efficient to compile on 1972 hardware, but it's not inherently less ambiguous.
    – Steve
    Jun 18, 2023 at 7:49

2 Answers 2

2

I disagree with the premise here.

In languages that allow "forward references" (i.e. usage of a function whose declaration is only mentioned later in the source code), the typical approach is not to treat them as implicitly declared at the point of usage, but to parse the entire file looking for declarations first, and only then (once there is a complete list of declarations available) analyse usages.

In other words, the compiler does not work strictly top to bottom, but instead makes multiple passes over the whole, and stores intermediary results.

I'm more familiar with languages where variables can be implicitly defined - that is, there need be no explicit declaration at all, and the type of the variable is inferred from the type of the value first assigned.

When functions are defined without the usual ceremony of declaration occuring anywhere in the source code, they are more often called "anonymous" rather than "implicitly declared".

As for advantages and disadvantages, it's not clear at the time of answering whether we would be considering forward references, implicit variable declaration, or anonymous function declaration.

1

In many modern languages function declarations need not precede the function use. But what that means is that the compiler just has to work harder and somehow find these declarations. And of course the compiler writer has to work a lot harder to make this happen. This doesn’t introduce any ambiguity, the function must be there, it will be found, and everything will be checked correctly.

There was an “implicit declaration” in old C. When the compiler saw something that looks like a call to an unknown function it would generate a function declaration. Sometimes this declaration wouldn’t be good enough to actually work, but if you knew the rules as a programmer, you could write functions that matched their implicit declarations well enough. Not quite satisfactory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.