Thread: functions (advantages/disadvantages)

  1. #1
    Registered User
    Join Date
    Apr 2004

    Post functions (advantages/disadvantages)

    i have a question on functions (void and return).

    let's say i have function that reads an integer [read_num()]. should i use a return function [int read_num(int)] or a void with pass by reference [void read_num(int &)]?

    does it make any difference or is it just preference?
    what are the advantages and disadvantages of using void or return?

  2. #2
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Plano, Texas, United States
    mostly it is just preference.

    personally i like to use returns, because it seems to me that it is much easier to write:

    int num = read_num ( );

    rather than:

    int num;
    read_num ( num );

    It also depends how you use it. Sometimes you might need to make it a reference parameter instead of being returned.

    Overall, however, it is a matter of preference. I vote for the return statement. It can be much more useful in my opinion. For example, if you used a return statement, you could do this:

    cout << read_num();

    However, if you used a reference, you would have to do this:

    read_num ( num );
    cout << num;
    My Website

    "Circular logic is good because it is."

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    For something like that, you need to return two bits of information
    - success/fail/error status
    - the actual value if success
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    even though you're returning more information, I usually use a return if I'm just returning one thing...
    Join is in our Unofficial Cprog IRC channel
    Channel: #Tech

    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM