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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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