Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

c++

c

c#

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?

like image 331
norris Avatar asked Feb 07 '10 19:02

norris


4 Answers

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...

like image 126
Thomas Levesque Avatar answered Oct 05 '22 22:10

Thomas Levesque


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

like image 38
DigitalRoss Avatar answered Oct 05 '22 22:10

DigitalRoss


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++.

like image 41
Rune FS Avatar answered Oct 06 '22 00:10

Rune FS


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.

like image 31
Douglas Leeder Avatar answered Oct 05 '22 23:10

Douglas Leeder