Binding association of a ‘function definition’ to a ‘function call’ or an association of a ‘value’ to a ‘variable’, is called ‘binding’. During compilation, every ‘function definition’ is given a memory address; as soon as function calling is done, control of program execution moves to that memory address and get the function code stored at that location executed, this is Binding of ‘function call’ to ‘function definition’. Binding can be classified as ‘static binding’ and ‘dynamic binding’.
If it’s already known before runtime, which function will be invoked or what value is allotted to a variable, then it is a ‘static binding’. if it comes to know at the runtime, then it is called ‘dynamic binding’.
Content: Static Binding Vs Dynamic Binding
Comparison chart
Basis for comparison | Static Binding | Dynamic Binding |
---|---|---|
Event Occurrence | Events occur at compile time are "Static Binding". | Events occur at run time are "Dynamic Binding". |
Information | All information needed to call a function is known at compile time. | All information need to call a function come to know at run time. |
Advantage | Efficiency. | Flexibility. |
Time | Fast execution. | Slow execution. |
Alternate name | Early Binding. | Late Binding. |
Example | Overloaded function call, overloaded operators. | Virtual function in C++, overridden methods in java. |
Definitions of Static Binding
When compiler acknowledges all the information required to call a function or all the values of the variables during compile time, it is called “static binding“. As all the required information are known before runtime, it increases the program efficiency, and it also enhances the speed of execution of a program.
Static Binding makes a program very efficient, but it declines the program flexibility, as ‘values of the variable’ and ‘function calling’ are predefined in the program. Static Binding is implemented in a program at the time of coding.
Overloading a function or an operator are the example of compile-time polymorphism, i.e. static Binding.
Implementation
#include <iostream> using namespace std; class overload { int a, b; public: int load(int x) { // first load() function. a=x; cout<<"The value of x is "<<x<<"\n"; return a; } int load(int x, int y) { //second load() function. a=x; b=y; cout<<"The value of x is "<<x<<"\n"; cout<<"The value of y is "<<y<<"\n"; cout<<"The value of x x y is "<<a*b<<"\n"; return a*b; } }; int main() { overload O1; O1.load(20); //This statement binds the calling of function to 'first' load() function. O1.load(20,40); //This statement binds the calling of function 'second' load() function. }
Now, here the statement’ O1.loads(20)’ clearly binds the function calling to first ‘load(int x)’ function as it is the only function which accepts single integer argument. The statement O1.loads(20,40) binds the function calling to second ‘load(int x, int y)’ function as it is the only function which accepts two integer arguments. Hence, no time will be wasted in deciding which function to invoke; this will make program execution efficient and fast.
Definition of Dynamic Binding
Calling a function or assigning a value to a variable, at runtime is called “Dynamic Binding“. Dynamic Binding can be associated with run time ‘polymorphism’ and ‘inheritance’ in OOP.
Dynamic Binding makes the execution of program flexible as it can be decided, what value should be assigned to the variable and which function should be called, at the time of program execution. However, as this information is provided at runtime, it makes the execution slower as compared to static Binding.
Implementation
#include <iostream> using namespace std; class base { public: virtual void funct() { // Virtual function. cout<<"This is a base class's funct()"<<"\n"; } }; class derived1 : public base { public: void funct() { //overridden virtual function. cout<<"This is a derived1 class's funct()"<<"\n"; } }; class derived2 : public base { public: void funct() { //overridden virtual function. cout<<"This is a derived2 class's funct()"<<"\n"; } }; int main() { derived1 d1; derived2 d2; base b, *p; p=&b; p->funct(); //The above statement decides which class's function is to be invoked. p=&d1; // Vlaue of the pointer changes. p->funct(); //The above statement decides which class's function is to be invoked. p=&d2; // Again vlaue of the pointer changes. p->funct(); //The above statement decides which class's function is to be invoked. return 0; }
Here the value of the pointer changes as the program is in execution and the value of the pointer decides which class’s function will be invoked. So here, the information is provided at run time, it takes the time to bind the data which slowdowns the execution.
Key Differences Between Static and Dynamic Binding
- Events that occur at compile time like, a function code is associated with a function call or assignment of value to a variable, are called static/early Binding. On the contrary, when these tasks are accomplished during runtime, they are called dynamic/late Binding.
- ‘Efficiency’ increases in static Binding, as all the data is gathered before the execution. But in dynamic Binding, the data is acquired at runtime so we can decide what value to assign a variable and which function to invoke at runtime this makes execution ‘flexible’.
- ‘Static binding’ make the execution of a program ‘faster’ as all the data needed to execute a program is known before execution. In ‘dynamic binding’ data needed to execute a program is known to the compiler at the time of execution which takes the time to bind values to identifiers; hence, it makes program execution slower.
- Static Binding is also called early Binding because the function code is associated with function call during compile time, which is earlier than dynamic Binding in which function code is associated with function call during runtime hence it is also called late binding.
Conclusion
However, we conclude that when we have prior knowledge of the values of variable and function calling, we apply the static binding. Conversely, in dynamic Binding, we provide all the information at the time of execution.
Akash says
Thanks for this good information.. it is useful for my project work.. thank you so much
Derrick says
great content…..
Shimul Bhattacharjee says
Thank you.
jashan says
Thank you..