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’, and if it comes to know at the run time then it is called ‘dynamic binding’.
Content: Static 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 variable’ and ‘function calling’ are predefined in the program. Static binding is implemented in a program at the time of coding.
Static Binding makes a program very efficient, but it declines the program flexibility, as ‘values of 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 of static binding in C++ with example of overloading
- class overload{
- int a, b;
- public:
- int load(int x){ // first load() function.
- a=x;
- return a;
- }
- int load(int x, int y){ //second load() function.
- a=x;
- b=y;
- 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 run-time 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. But as this information is provided at run time it makes the execution slower as compared to static binding.
Implementation of dynamic binding using ‘virtual functions’ in C++.
- class base{
- public:
- virtual void funct(){ // Virtual function.
- cout<<"This is a base class's funct()";
- }
- };
- class derived1 : public base{
- public:
- void funct(){ //overridden virtual function.
- cout<<"This is a derived1 class's funct()";
- }
- };
- class derived2 : public base{
- public:
- void funct(){ //overridden virtual function.
- cout<<"This is a derived2 class's funct()";
- }
- };
- int main()
- {
- base *p, b;
- derived1 d1;
- derived2 d2;
- *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 slow downs 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, and 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 make execution ‘flexible’.
- ‘Static binding’ make 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 the prior knowledge of the values of variable and function calling, we apply static binding whereas, in dynamic binding, we provide all the information at the time of execution
No comments:
Post a Comment
commnet here