CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2007
    Location
    France
    Posts
    329

    Help with functions

    Sorry for the noob question but i still dont understand the benefits of a
    function that does something but returns nothing.
    Whats the point?

    Code:
    void echoSquare()
    {
    int value;
    cout << “Enter a value:”;
    cin >> value;
    cout << “\n The square is:” << (value * value) << “\n”;
    return;
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Help with functions

    It compartmentalizes your code to reduce clutter and allows you to call the same code from different parts of your program without having to retype it.

    Also useful in object oriented programming, where each object will be operating on its own data.

  3. #3
    Join Date
    Dec 2007
    Location
    France
    Posts
    329

    Re: Help with functions

    I think i didnt put it right.
    The question is not what's the use of functions.

    I asked whats the use of functions that returns nothing.
    You have functions that return a value and functions that return
    nothing at all.
    I dont see what good are they for.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Help with functions

    Quote Originally Posted by MasterDucky
    I think i didnt put it right.
    The question is not what's the use of functions.

    I asked whats the use of functions that returns nothing.
    You have functions that return a value and functions that return
    nothing at all.
    I dont see what good are they for.
    My answer has nothing to do with what they return.

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Help with functions

    Side-effects. Sometimes side-effects include printing something to the console; in the case of class member methods, they may modify class state. Sometimes they may change the value of a global variable, although this usage is discouraged most of the time.

    Also, sometimes values are "returned" simply by modifying parameters passed by reference (or pointer parameters), rather than via explicit returns.

  6. #6
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Help with functions

    To add to what Lindley and GCDEF said, a member function of a class may not return anything, but might modify the state of the object that it is operating on.

    Viggy

    PS. D'OH! I skipped that phrase in Lindley's response! Sorry!!!

  7. #7
    Join Date
    Dec 2007
    Location
    France
    Posts
    329

    Re: Help with functions

    Ah ok i see, i wonder why i didnt see this.
    I guess i got stuck on the fact that if it does something it has to give something...
    ...but it does give something...

    Anyway, thanks for the help!

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