norris
norris

Reputation: 417

Why does a function need to be declared before it's defined or used?

In C its optional. In C++ one "MUST" declare a function before its used/defined. Why is it so? Whats the need? We don't do that in C# or Java.

Funny thing is while we are defining a function. The definition itself has a declaration even then, we need to declare. God knows why?

Upvotes: 9

Views: 5122

Answers (5)

Rune FS
Rune FS

Reputation: 21742

Basically, it's down to how you write the compiler for the language. In C++, the decision has been to make a one pass compilation possible. To do that, you (or rather the compiler) need to be able to first read the declaration of all classes, methods and the like and then read the implementation (or in C++ terms, the definition). In Java and C#, the compiler first reads through all the code generating what corresponds to what the C++ compiler generates when reading the header files. The C#/Java compiler then reads the implementation (aka definition). So, in C++, the developer is asked to write the declaration whereas in C#, the compiler runs through the code multiple times doing the declaration work for the developer.

As an aside, other languages used to ask you to write the functions in the order you needed them (if function B uses function A, you have to define A first). Most of those languages had constructs to allow you to get around this. In (Turbo) Pascal, the solution was, in a kind, the same as in C++.

Upvotes: 5

DigitalRoss
DigitalRoss

Reputation: 146043

Java and C# specify both the language and the binary object file format, and they are multi-pass compilers.

As a result, they are able to peek at later definitions or those that were compiled separately.

C doesn't work this way for several reasons:

  • Without using managed code it is a lot harder to define a machine-independent object format with type information

  • C deliberately allows bypassing the type mechanisms

  • When originally defined, there generally wasn't enough memory to run sophisticated compilers, nor were there prototypes to read anyway

  • C programs must be arbitrarily large with system-specific library and search path mechanisms. All of this gets in the way of defining an object-module-based type system

  • Part of the C portability and interoperation basis is the "input language only" nature of the specification

  • Until recently, even the limited one-pass nature of C was still barely practical for large programs. Something like Java or C# would have been out of the question: you could take a vacation and your make(1) would still not be done

Upvotes: 8

Douglas Leeder
Douglas Leeder

Reputation: 53310

C++ vs. Java/C# - Single-pass compiler (C++) vs. multi-pass compiler (Java & C#). Multiple passes allow Java and C# compilers to peek at future types and functions prototypes.

C++ vs. C - The C feature to have default declaration is basically a bug, fixed in C++. It causes problems, and it is an enabled warning for gcc. In C++ the arguments form part of the function exported name (name-mangling), so must be known before the correct function can be called.

Upvotes: 2

dma_k
dma_k

Reputation: 10639

In C++ one "MUST" declare a function before its used/defined. Why is it so? Whats the need? We don't do that in C# or Java.

I would like to say, that is not true. Yes, in C++ you have to define a function signature (prototype), before referring to it. But you may leave the implementation for a later time.

In Java that does not work: you cannot call the method of some class without having that class compiled (note: together with implementation) and available in javac classpath. So, Java is more strict in this sense.

Upvotes: -3

Thomas Levesque
Thomas Levesque

Reputation: 292415

Funny that you mention that, just this week Eric Lippert wrote a blog post related to your question :

http://blogs.msdn.com/ericlippert/archive/2010/02/04/how-many-passes.aspx

Basically, this is related to how the compiler works. The C# and Java compilers make several passes. If they encounter a call to a method that is not yet known, that's not an error, because the definition might be found later and the call will be resolved at the next pass. Note that my explanation is overly simplistic, I suggest you read Eric Lippert's post for a more complete answer...

Upvotes: 22

Related Questions