Definition Vs Implementation

Hello Guys,

I just have a doubt and that is, when you simply do the implementation of method itself like in C#, above main, all works perfectly. So why do we have to first prototype the method above main and then do the implementation of the same method below the main? Is it not too many lines of code?

Thanks & Regards,
Bhanu

Hello there,

The short answer is that C++ is old. Like made in the eighties old (old for a programing language anyways!)

The longer answer is that C++ for the most part is read differently than an interpreted language like C#. C# is what is known as a high-level programming language, or simply, a more human-readable language. The code is compiled at run time, and the compiler does a lot of the weird bits for you, such as making sure methods are defined before they are used. It makes coding much easier, but there are some drawbacks. Interpreted languages are bit slower in performance and a lot of them don’t give you much control over individual blocks of memory.

C++, on the other hand is a compiled language, which simply means it is translated to machine language before run-time. While it is a bit higher level than something like Assembly or heaven forbid binary, C++ more closely resembles a machine-readable language than a human-readable one. Thus, it conforms to some of the same rules of machine language, which can be though of as executing linearly. Executing a function before it is defined (ie defining the function below the main function) would be like reading a chapter in a book that is still on the shelf. However, implementing our functions above the main function would be too cumbersome, and not very intuitive, so to get around the issue we declare them before hand.

I am perhaps over simplifying a few things, but I hope that gives you some idea of the difference.

Cheers!

That does simplify. Thanks a lot

Privacy & Terms