Page 1 of 1

Declarations vs Definitions

#1 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Posted 01 May 2010 - 03:00 PM

DECLARATIONS V.S. DEFINITIONS


Declarations and definitions are two terms that seem very similar. In fact, some people think that they are synonyms. The reason for such a confusion is because of the fact that most of the time, when we are declaring something, we are also defining. However, it is very important that we understand the differences between the two and know when we are declaring something and when we are defining something.

Let's start by examining a piece of code:
int number; // definition or declaration?

A definition is a term we use to say that we are defining attributes of something that exists.

A declaration is a term we use to say that we are telling the compiler that something exists. We are declaring the existence of something to the compiler. So are we performing a declaration? Yes, but let's take a closer look at what exactly happens.

int number; allocates memory for the variable number. In other words, we are defining memory space for our variable. We are defining the variable's attributes because we are saying, "this memory space is for number".

The action that we are performing is a definition, because of what we are essentially doing: defining memory space for the variable. However, in doing so, we are also making the compiler aware of the variable's existence.

int number; // definition, implicit declaration


Most people tend to differentiate between declarations and definitions by remember that declarations declare existence and definitions define memory space. Remembering it this way is part true, but also part incorrect.

Let's examine a short piece of code:
void function(); // function prototype

It allocates memory for the function, but it is not a complete definition. A complete function definition tells us what the function does; tells us what the body of the function looks like. Without defining a function we won't be able to use it.


Let's examine another piece of code:
class Foo
{
public:
  void Function();
};



Some people would say that it is a class declaration, because its methods are only prototypes and aren't defined. But remember the meaning of "declarations" and "definitions". Declarations tell us about something's existence whereas definitions tell us more about something. If you examine the piece of code closer, we actually are defining what a class looks like. Although the class' methods are prototypes and only declarations, it is still a class definition.

A class declaration would look like this:
class Foo;

The only practical use is so that we know that Foo is an existing class, however, without a definition, it is practically useless.


Let's take a look back:
// Declarations
class Foo;
void function();

// Definitions
class Foo
{
public:
  void Function();
};
void function()
{
  // function body.
}

int number; // implicit declaration



Notice that number is a definition but also a declaration implicitly. As you can see defining and declaring is quite coupled together when dealing with variables. This is a problem when you want to tell the compiler that a variable exists. For instance, if I know that number exists and I want to use it, I must let the compiler know it exists, but writing int number; not only declares it (informing the compiler of its existence) it also defines it. Thus, we are defining number twice, which is an error. Definitions must only be done once.

To remedy this issue, we make use of the extern keyword. Basically, the keyword tells the compiler that it is already defined in some other file.
extern int number; // declaration only, not a definition

To the compiler, it is just a declaration and no definition is being done.


To wrap things up, you need to think about declarations and definitions logically. Am I declaring the existence of something or am I defining what something looks like? I hope this tutorial clears up the confusion between declarations and definitions.

Is This A Good Question/Topic? 0
  • +

Replies To: Declarations vs Definitions

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Posted 01 May 2010 - 03:59 PM

Moved to Software Development. While the examples are in C++, the information applies to nearly all high level languages.




Also edited the title: v.s. to vs
Was This Post Helpful? 0
  • +
  • -

#3 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Posted 02 May 2010 - 01:40 PM

Good!
Was This Post Helpful? 0
  • +
  • -

#4 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1255
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Posted 12 May 2010 - 07:57 PM

Quote

Most people tend to differentiate between declarations and definitions by remember that declarations declare existence and definitions define memory space. Remembering it this way is part true, but also part incorrect.


I think this is incorrect when taken in the context that most people view the terms in. A declaration is simply notifying the compiler that you'd like some named address space set aside for you. A definition tells the compiler about how something is so to be implemented.

For most languages, the declaration serves to do some or all of the following:
Associate a name with a memory location.
Give an object/variable context for scoping and resolution.
Associate a data type with a memory location.

For most languages, the definition serves to do some or all of the following:
Assign value or state to an object/variable.
Associate an implementation with a class (class definition).
Associate an implementation with a function or method (function definition).

Often times, you are able to declare and define at the same time. Sometimes declarations are implicit.

Quote

Let's examine a short piece of code:
void function(); // function prototype


It allocates memory for the function, but it is not a complete definition. A complete function definition tells us what the function does; tells us what the body of the function looks like. Without defining a function we won't be able to use it


Compilers don't allocate memory for functions in the same way they do for variables. Rather, functions are translated into machine code (or some other language) which will then be loaded into memory. The only information a compiler could gather from the prototype is the signature (return type, name, and parameter types). It has no idea how large the body of the function will be so it can't possibly allocate memory for it. The previous code snippet is therefore a function declaration. It would need a function definition to be included in the executable.

Quote

int number; // definition, implicit declaration

This is actually just a declaration, no value or state has been assigned to the variable yet. Nor is this declaration implicit as the data type is explicitly declared.

Quote

int number; not only declares it (informing the compiler of its existence) it also defines it. Thus, we are defining number twice, which is an error. Definitions must only be done once.

Actually, you can redefine the state of a variable as many times as you want during the execution of a program. You can usually only declare a same-named variable once in the same scope. Hence why the compiler would throw a "Redeclaring variable of type X" error.

This post has been edited by Dark_Nexus: 12 May 2010 - 08:12 PM

Was This Post Helpful? 0
  • +
  • -

#5 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Posted 02 July 2010 - 02:57 AM

Quote

Actually, you can redefine the state of a variable as many times as you want during the execution of a program. You can usually only declare a same-named variable once in the same scope. Hence why the compiler would throw a "Redeclaring variable of type X" error.


class A{};
class A{}; // Redefinition error.

class A;
class A; // valid.

int B;
int B; // error C2086: 'int a' : redefinition



Why would same-named variables only be allowed to be declared once but same-named class can be declared multiple times?

B is an instance of type 'int', requiring allocation of memory. That is why it is implicitly defined.

Redeclaration should not be an error. Redeclaring the existence of something should not be wrong:
class A;
class A;
void B();
void B();
int C;
int C; // ?!?!?! why error!



Perhaps because you are defining the type of the instance 'C'.

What you are saying is very logical and I used to think int a; is just a declaration, the following error is rather questionable and inconsistent:
int a;
int a;


Quote

Compilers don't allocate memory for functions in the same way they do for variables.

I am sure you are right, in fact... I wasn't so sure memory was being allocated.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1