Thread: How to avoid writing more code in a function?

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    49

    How to avoid writing more code in a function?

    Good afternoon,

    Consider the following function:

    Code:
    double calculate(){
      double ret;
      for (int a = 0; a<=10; a++){
        ret = 2*a;
      }
      return ret;
    }
    This is just a hypothetical function where a variable (here, ret) constantly changes its value.
    Suppose I need to print the steps of the function; for example, print every value of ret, and put everything between parentheses:
    Code:
    double calculate(){
      double ret;
      printf("(");
      for (int a = 0; a<=10; a++){
        ret = 2*a;
        printf("%d", ret);
      }
      printf(")");
      return ret;
    }
    (codes not tested in a compiler)
    So, in a few words, I have a function that makes calculations, and I want to output the result of each step of the calculation to a file. But the problem is that I don't want to "fill" the function with a lot of output functions, like sprintf, printf, fprintf... I just want the function to have the calculation procedure; otherwise, if I need to use the function somewhere else and didn't need the outputs, I would have to erase them all.
    Any idea on how I could track and output the steps of the function and keep it "clean" from lines that output to screen/files?
    Tell me if I'm not being very clear.
    Thank you in advance.
    Last edited by pc2-brazil; 12-16-2009 at 02:21 PM.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    You could introduce a parameter with a default value, and then use the value of that to determine whether you should print anything or not.

    Code:
    double calculate(const bool print = false)

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    For one thing you might use macros, e.g

    Code:
    double calculate()
    {
    #ifdef VERBOSE_VERSION
        putchar('(');
    #endif
       ...etc
    }
    or

    Code:
    #include <iostream>
    
    //#define VERBOSE_VERSION
    
    #ifdef VERBOSE_VERSION
    #define OUTPUT_VALUE(n) std::cout << n << ' '
    #else
    #define OUTPUT_VALUE(n) (void) 0
    #endif
    
    
    double calculate()
    {
        OUTPUT_VALUE('(');
        double ret;
        for (int a = 0; a<=10; a++){
            ret = 2*a;
            OUTPUT_VALUE(ret);
        }
        OUTPUT_VALUE(')');
        return ret;
    }
    
    int main()
    {
        calculate();
    }
    In general, I think that's what the debugger is for.
    Last edited by anon; 12-16-2009 at 03:00 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    49
    Thank you for the help.
    Macros seem a good idea; I've never used them. I will try and ask here if something goes wrong.
    The only problem is that it would still "pollute" the calculation code a little.
    anon: yes, that's what the debugger is for, but, actually, debugging is not my intention.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by pc2-brazil
    debugging is not my intention.
    Then what is your intention?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Alternatively you could have the function call out to an IOutput interface.


    Code:
    class IOutput
    {
    
    public:
    virtual ~IOutput() { } virtual void OutputString(const std::string &text) = 0;
    }; double calculate(IOutput *pOutput) { pOutput->OutputString("("); std::ostringstream ostrm; double ret; for (int a = 0; a<=10; a++) { ret = 2*a; ostrm << ret << std::endl; pOutput->OutputString(ostrm.str()); ostrm.str(""); } pOutput->OutputString(")"); return ret; }
    Now the function could care less about how the output is implemented. As long as the object passed in derives from IOutput and implements OutputString() it could output to a disk, monitor, printer, etc, etc and the function body never has to change.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems installing Mingw5.1.4
    By BlackOps in forum C Programming
    Replies: 2
    Last Post: 07-26-2009, 03:28 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM