Answer to Question #186918 in C++ for zaid

Question #186918

Declare and initialize a static variable as counter in a function (increment it in every call) and call it multiple times printing the change in value


1
Expert's answer
2021-04-28T20:16:34-0400
#include <iostream>


using namespace std;




void someFunction()
{
    static int counter = 0;


    cout << "Calling function" << endl;
    counter++;


    cout << "counter = " << counter << endl << endl;
}


int main()
{
    for(int i = 0; i < 5; i++)
        someFunction();


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS