Explain the Difference Between Definition and Declaration

Computer ProgrammingProgrammingMiscellaneous

In this post, we will understand the difference between definition and declaration.

Definition

  • Definition, with respect to a function indicates that the body of the function has been developed.

  • With respect to variable, it means a value has been associated/defined with that variable.

  • A function can be defined only once.

  • Once a function or variable has been defined, it is ready to be used.

  • A variable can be re-defined multiple times, as and when required. This depends on the language and the scopes.

  • Memory is allocated when function or a variable is defined.

Below is an example of variable definition

sum = 0

A variable named ‘sum’ is assigned to 0.

Below is an example of function definition

def add_val(param_1, param_2)
c = param_1 + param_2
return c

The ‘add_val’ is a method, and ‘param_1’ and ‘param_2’ are parameters passed to it. There is a definition of this method too.

Declaration

  • Function declaration basically means that a name has been given and the parameters have been mentioned.

  • No value is associated with a variable when it is just declared.

  • Its body hasn’t been developed yet.

  • Once a function or variable has been declared, it is not yet ready to be used.

  • A function or variable can be declared any number of times.

  • Memory isn’t allocated when a function or variable is declared.

Below is an example of variable declaration −

sum_val

A variable named ‘sum_val’ is declared.

Below is an example of function declaration −

add_val(p_1, p_2)

The ‘add_val’ is a method, and ‘p_1’ and ‘p_2’ are parameters passed to it. There is no definition of the method.

raja
Published on 23-Mar-2021 11:22:07
Advertisements