You are here:

C++/Why must variables be declared before it is used, but not methods?

Advertisement


Question
Hi,

Why must variables be declared before it is used, but not methods?

Thanks for your help,
lzzzz

Answer
Wrong. In C++ functions and variables have to be declared before use. They need not be defined however. In C++ you can declare something as many times as you like but you can only define it once (the one definition rule). In C for functions first use (call) of a function determined its signature and hopefully this matched the actual signature. However C has no function overloading which means it could just use the name for the function as a signature and hope the parameters are correct...

In C++ which function is called is determined by the arguments and which is picked is a tricky affair so you have to declare a function before it is used or rather before it can be considered for use.

In most cases in C++ defining something also declares it. So:

void funct();

is a function declaration.

void funct()
{
// code...
}

is a function definition and declaration quite probably in an implementation source file totally separate from most callers of the function.

extern int GlobalVariable;

is a variable declaration.

int GlobalVariable(234);

is a variable definition which also declares it. In the case of variables or objects only declarations for global variables having external linkage makes sense. Locals and global statics (having internal linkage) always have definitions as they are always used in the same compilation unit (i.e. source file) they are defined in.

The compiler needs enough information to perform operations on the things you use in your code. How much information is required depends on the use. For a variable the compiler needs to know what storage to assign and how to initialise the object (for class types of course this is which constructor to call). For items such as global variables and non-static functions (note: this includes static class member functions) that have external linkage the compiler only needs a declaration it is assumed the linker will resolve the external references to such items replacing them with the actual function or variable reference. This assumes that somewhere each such external item  has a definition and only one definition and that it is presented to the linker when linking your code to create an executable.

I am sure I ranted on at length about the differences between declarations and definitions in a reply some time ago to you. Please go and look it up and get the difference straight in you mind it is useful to do so!

Note this is a simplistic answer and does not take into account complexities created by the likes of inline function definitions and class and function template specialisations.

Now if you will excuse me I have to do a load of paperwork...Sunday is _not_ a good time to ask me questions if you want very long answers as I simply do not have the time.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.