You are viewing a single comment's thread. Return to all comments →
Whats the significance of using static int here?
Using just int gives out error so whats the difference between static int and int and why we using it in this algo?
Static variables are executed only once during the entire lifetime of the program unlike normal variables. In the recursive function if we use a normal variable then counter won't be updated as everytime the function is called that variable is set to 0. So we use static variable here as it will only be executed once irrespective of the no of function calls made. So that counter is incremented by 1 everytime the recursive function is called.