Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 865589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:39:42+00:00 2026-05-15T09:39:42+00:00

Keeping track of how many times a function is called is easy when passing

  • 0

Keeping track of how many times a function is called is easy when passing the counter as an argument into the function. It’s also easy when returning a one from the called function. But, I do not want to go that route. The reason behind this is because it seems like bad programming (letting the function know too much information). Is there a better way to keep track of how many times this function has been called?

I’m just looking for concepts that I could study. Providing code examples is not neccessary, but might be helpful.

Edit: I’m not actually looking for profiling tools. Let me add some code to get my point across. Because scope for funcCounter ends in main, I have no way of getting back a variable from myFunction that will increment funcCounter. I could possibly return 1 from myFunction and then increment funcCounter that way, but this doesn’t seem like very good programming. Is there another way to do it?

int main()
{
int funcCounter = 0;
char *mystring = "This is a silly function.";
myFunction(mystring);
cout << "Times function is called: " << funcCounter << endl;
    return 0;
}

void myFunction(char *mystring)
{
cout << mystring << endl;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:39:43+00:00Added an answer on May 15, 2026 at 9:39 am

    Have a static variable in your function and keep incrementing it each time the function in called.

    void my_Function(void) {
        static unsigned int call_count = 0;
        call_count++;
    }
    

    If you want to do it for debugging reasons, then there are tools like gcov which do this for you. (I’m pretty sure Microsoft doesn’t have an alternative bundled with Microsoft Visual C++)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How could I go about keeping track of the number of times a word
I've always found checkin (commit) mails to be very useful for keeping track of
Unfortunately, my question is not as simple as keeping track of two windows created
Keeping the GUI responsive while the application does some CPU-heavy processing is one of
I'm curious about keeping source code around reliably and securely for several years. From
( Almost exact duplicate of Keeping original format POST passing through AWK submitted by
Keeping performance on a mobile device in mind, what do you think is the
Is keeping JMS connections / sessions / consumer always open a bad practice? Code
Are primitives worth keeping? Should all the deprecated stuff be deleted? Do we need
What are the advantages/disadvantages of keeping SQL in your C# source code or in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.