What is The Difference Between Declaration And Definition of a Variable/Function?

Declaration of a variable/function simple declares that the variable/function exists somewhere in the program but memory is not allocated them. But the declaration of a variable/function serves an important role. And that is a type of the variable/function. Therefore, when a variable/function is declared, the program knows the data type of that variable. In case of function declaration, the program knows what are the arguments to that functions, their data types, the order of arguments and the return type of the function. So that’s all about declaration. Coming to the definition, when we define a variable/function, apart from the role of declaration, it also allocates memory for that variable/function. Therefore, we can think of definition as a super set of declaration. (or declaration as a subset of definition). From this explanation, it should be obvious that a variable/function can be declared any number of times but it can be defined only once. (Remember the basic principle that you can’t have two locations of the same variable/function).

Comments

Popular posts from this blog

How to Create Parrot OS Style Terminal/Bash Prompt on Any Linux Distro

What is Linux?