CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2013
    Posts
    6

    Good practice vs bad practice (void vs int functions)

    Dear all,

    I have a question regarding what a good practice is when working with functions. I am designing a very simple function for addition. I know that what will be the result of that addition is an integer, hence declaring an int function seems to be the right way of doing it. That said, I bumped into the fact that if I declared the same function as void when I run the program it seems to work all right!

    here a contrast of what I've seen and found:
    -------------------------------------------------------------------------------
    int addition (int x, int y, int result){
    result = x + y;
    cout << "The result of your sum is " << result << endl;
    return result;}
    -------------------------------------------------------------------------------
    void addition (int x, int y, int result){
    result = x + y;
    cout << "The result of your sum is " << result << endl;}
    --------------------------------------------------------------------------------
    Question here is: What is the difference and what implications can this kind of practice have in the long run?

    Cheers!

    FX
    Last edited by FelixCast; March 29th, 2013 at 12:30 PM.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Good practice vs bad practice (void vs int functions)

    In both your examples, you print the result of the sum. Now, imagine if I wanted to use your function and write the result to a file, but without printing the result. I can't do that. But if you write your function as:
    Code:
    int addition(int x, int y) {
        return x + y;
    }
    Then you can print:
    Code:
    cout << "The result of your sum is " << addition(100, 200) << endl;
    and I can write to file without printing, and we both get to call your function. Furthermore, when you want to print, your second example can come into play:
    Code:
    void print_addition(int x, int y) {
        cout << "The result of your sum is " << addition(x, y) << endl;
    }
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Mar 2013
    Posts
    6

    Re: Good practice vs bad practice (void vs int functions)

    Right, I get it now. Now then, would you say that including "cout" calls within functions is correct or is there a rule of thumb to follow?

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Good practice vs bad practice (void vs int functions)

    Quote Originally Posted by FelixCast
    Now then, would you say that including "cout" calls within functions is correct or is there a rule of thumb to follow?
    If your intention is to write a function whose purpose is to print to standard output, go ahead. Nothing wrong with that.

    Later on, you may find that sometimes it is better to have a std::ostream reference parameter for certain functions so that you can opt to print to standard output, or to a file, or to some other output stream.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    May 2009
    Posts
    2,413

    Re: Good practice vs bad practice (void vs int functions)

    Quote Originally Posted by FelixCast View Post
    Now then, would you say that including "cout" calls within functions is correct or is there a rule of thumb to follow?
    There's an important design principle called Separation of Concerns that applies here. It's the basis of modularisation for example.

    http://en.wikipedia.org/wiki/Separation_of_concerns

    I'd say it's good practice not to intertwine output code with calculations. It's better to keep these "concerns" separated so each can be considered independently.
    Last edited by nuzzle; March 29th, 2013 at 02:27 PM.

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Good practice vs bad practice (void vs int functions)

    In addition to the SoC concept that Nuzzle mentioned is to consider an error handling strategy.

    Should you return an error (probably not good if you are adding two integers)? Should you throw exceptions?

    The main thing is to develop a consistent error strategy within your program so you can catch and report errors.

  7. #7
    Join Date
    Mar 2013
    Posts
    6

    Re: Good practice vs bad practice (void vs int functions)

    Thank you so much for your really good answers guys, it's brilliant to be a beginner and not get the snub from real programmers!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured