What is the difference between statement and definition?

Asked

Viewed 4,057 times

20

These things seem to be the same thing. Are they? And assignment is different? The terms are interchangeable.

1 answer

18


Statement

Declaration is something the code does to show the compiler that an identifier will exist in the code. It can be a variable, function, label, data structures (such as classes, for example), among others. In general it only occurs once in the code (in some languages it is even possible to declare more than once in different files as external identifier).

int x;
int f(int x, int y); //varia de linguagem para linguagem, mas note o ponto e vírgula
class Exemplo; //só para dizer que a classe existe

Some languages (dynamically typed) do not require formal declaration of the variable before its use.

In statically typed languages the declaration involves informing the type of the variable. Some languages may infer this. The declaration is important to reserve space in memory (stack and/or heap) for the data that the variable will support.

Some languages say that the declaration only occurs as prior information (usually externally). Only during the definition will there be memory reserve. It makes some sense, but informally people speak in declaration all the time, and everyone understands that there reserves the memory, so I preferred to put it this way. But know that formally the term is used otherwise.

Attribution

Assignment only occurs with variables. It can occur several times in the code, whenever you need to put a new value to the variable. It may occur next to the statement.

x = 10 * Sin(30) + 5; //x já estava declarada antes
//faz algo com x
x = 0; // nova atribuição

The first assignment can be called initialization.

Most languages initialize the variable in the declaration with a value default. A few require initialization to be done, either in the declaration, or before the first use. There are still some that allow the variable to be used without a formal initialization, taking the value that is in memory in the place reserved for it (which is a danger). There are cases that depend on the context.

Objects

Note that a variable can have a proper value that is a reference to another object. Many languages do this transparently, or at least automatically during variable initialization, others may require, or at least allow, these operations to be independent. Then the creation of the referenced object is done manually by the programmer, assigning the reference to the variable.

The object itself also needs to be initialized and each language has a criterion to do so automatically or not.

Some languages only allow the code to directly manipulate the value of the object and not the variable (in the case of variables with references).

Definition

Variable

Although I know nothing formal saying this, I consider that a attribution made during the statement be a definition variable. Some people prefer to call this initialization. I don’t like the term so much because the initialization can be considered the first assignment, which can occur after the declaration.

int x = 1; //declarando e atribuindo.
var x = 2; //inferiu o tipo durante a inicialização
x : int = 3; //algumas linguagens possuem sintaxe diferente, claro
x := 4; //simples, né? O : diferencia a declaração de uma atribuição

Type inference usually occurs only during a full definition of variable. Some languages have more sophisticated algorithms and make the inference in future assignment of the variable (so you can even do with parameters).

Data structures and functions

The term definition is widely used with functions and data structures. Defining is putting the implementation code.

Often the declaration occurs at the time of definition. In fact few languages require the declaration to be separated from the definition. Some require in some circumstances when there are circular references. Some compilers work in two steps and solve this on their own.

int f(int x, int y) { return x + y; } //não tem ; no fim, mas está declarando também
class Exemplo { ... //membros aqui } //algumas linguagens exigem o ; final, outras não
Exemplo::f(int x, int y) { return x + y; } //algumas linguagens definem as funções fora

There’s a controversy there. Some people consider that even when you put members within the data structure, it’s still a statement. So in this case there is no definition of data structures. One can define variables or objects that will use it as a basis, but it itself is only stated. I think it’s weird because the structure can have defined functions. I don’t know, maybe it depends a little on the language.

Some say that definition only occurs after having a prior statement. I find it confusing. Something changes its name based on something you don’t even know exists?

Instantiation

This is a slightly different concept, but it is useful to understand as well. As comments, there may be confusion with this.

Instantiating an object is putting a value somewhere in the memory. This data will have a specific format. Even dynamic languages need to have a format for instantiated data.

Instantiation generates objects. Where it will be stored depends on what is desired. It can be in the position of the variable itself (types by value) or there goes a pointer and the object is placed in another area (types by reference). See more in Memory allocation in C# - Value types and reference types and What’s the difference between Struct and Class? - is C#, but gives an idea that is worth to a greater or lesser degree in other languages.

A variable can only have one instance of a type at a time. But it is possible to swap the instance used in the variable whenever you want. This occurs with an assignment. Here it may be useful to know What "immutable" really means?.

Instances do not need to be linked to variables, they can be used intermedially and be discarded without storage (except temporary), are very ephemeral.

Completion

Some languages may use the terms differently according to their specification.

It may sound silly, but terms are important for good communication between programmers. The answers here themselves can talk about this and if the programmer doesn’t know exactly what it means it can get a little confusing.

They are often used interchangeably, but can lead to confusion. It is best to use precisely, except when accuracy is of no benefit. But do not underestimate the benefits of correct communication.

  • If I understood correctly, the statement would be at development time (writing the code in the IDE) and the definition in the compilation/execution, ie the statement is recipe with the ingredients and the definition how the ingredients are combined (mémoria allocation) and applied? The terms seem synonymous with a brain fry :D

  • @rray I created this puppy because there is a lot of confusion on the subject. I tried to show that there are differences between the terms, even if they think that everything is the same. Both occur together. All of this is written in the code. I’m talking about code, not execution. Even the memory allocation, I’m talking about the code that tells you to do this and not the allocation itself. If you’re talking about class definition, I think it’s the recipe c/ingredients and way of preparation. The variable statement is "I’ll make the recipe such in this pan". The assignment is to throw all ingredients in the pan. The definition is to make both together.

  • "Instantiation" would be a form of definition then?

  • 1

    @diegofm think it can be used with attribution in certain circumstances. You can swap the instance of the variable with a simple assignment, it does not need to be in the variable definition (it is common to be). Instantiating is creating the value in memory. Note that it is possible to instantiate without assigning to some variable, so it is something else. Maybe it’s up to me to put this in the answer. I’ll think a little bit about.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.