Thread: Variable Scope

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    27

    Post Variable Scope

    Hi All,

    This is my question:

    Write a function that counts the number of times it is called. Name the function count_it(). Do not pass it anything. In the body of count_it(), print the following message:

    The number of times this function has been called is: ###

    where ### is the number. (Hint: Because the variable must be local, make it static and initialize it to zero when you first define it.)


    Here is what I did... Is there any way to make it better? different way?

    Thank you
    Code:
    #include <iostream>
    using namespace::std;
    
    count_it(); //prototypes
    second();
    
    main()
    {
    count_it();
    second();
    return 0;
    }
    
    
    count_it() // this function counts how many time it has been called
    {
    static int count = 0;
    count++;
    cout << "The number of times this function has been called is " << count << "\n";
    
    return 0;
    }
    
    second() //another function that calls count_it() 
    {
    	count_it();
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    1) Specify the return type of your functions.

    2) To better demonstrate what your count_it() function does, create a for loop in main() that loops 10 times, and inside the for loop call count_it().

  3. #3
    myNegReal
    Join Date
    Jun 2005
    Posts
    100
    I see no reason for the second() function. If you want to make it call twice, just call count_it() twice. If you don't want to pass a variable, and keep the variable locally in the function, I don't see many other ways you can do it. However, there's some more complicated ways to do it without passing anything to the function, but using a global variable.

  4. #4
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Ganoosh
    I see no reason for the second() function.
    It was an example of how he wants to use count_it(), in every function he may have.. whether it does nothing (as his does) or is some start screen for a game.

    I'd probably just use a global variable instead of a function and dump the cout, but it doesnt really matter.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    66

    more convenient way

    Code:
    unsigned long count_it()
    {
      static unsigned long count = 0;
      ++count;
      cout << "The number of times this function has been called is " << count << "\n";
    
      return count;
    }
    Unsigned int is a lot nicer for counters than a signed int.

    Returning the count is a nice convenience feature. I would actually make a class to wrap the counting and allow access to count.

    For example:

    Header:
    Code:
    class CallCounter
    {
    public:
      static unsigned int increment() { return ++m_count; }
      static unsigned int getCount() const { return m_count; }
      static void reset() { m_count = 0; }
    
    private:
      static unsigned int m_count;
    };
    Source:
    Code:
    unsigned int CallCounter::m_count(0);

    Usage:
    Code:
    void myFunc1()
    {
      CallCounter::increment();
    }
    
    void myFunc2()
    {
      std::cout << "Called " << CallCounter::increment() << "times." << std::endl;
    }
    
    void myFuncReport()
    {
      std::cout << "Total called " << CallCounter::getCount() << "times." << std::endl;
    }
    
    int main()
    {
      myFunc1();
      myFunc2();
      //...
      myFuncReport();
    
      return 1;
    }

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    int main()
    {
        
    
    
        return 0;
    }
    Code:
    using namespace std;
    Last edited by 7stud; 06-22-2005 at 10:48 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making a wstring variable global scope
    By stanlvw in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2008, 02:25 PM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Variable scope
    By Axel in forum C Programming
    Replies: 2
    Last Post: 09-19-2005, 08:41 PM
  5. variable in file scope ???
    By howhy in forum C++ Programming
    Replies: 5
    Last Post: 08-30-2005, 04:46 AM