Thread: Correct/best way to return values from a function?

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    107

    Correct/best way to return values from a function?

    Say I wanted to return a string I can do

    Code:
    string parseFilename(string fileName){
    
    return fileName.subtr(/*blah*/);
    
    }
    Can I also use pointers/references here though? When would you use a pointer and when just a return statement, any general tips here, some examples for instance?

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    With the string you probably want to pass a reference to the object instead, no return type required in that example
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by rogster001 View Post
    With the string you probably want to pass a reference to the object instead, no return type required in that example
    Not necessarily true. I would return a string.
    Pre-C++11, RVO would kick in, thus making the return free, given that your function is simply enough, of course.
    If your function is too complex, and your are using C++11, move copy would kick in, making it pretty much free anyway.
    Regardless, win-win. No need for references.

    As for pointers or references in the return statements...
    I'd use references if I need to return a local member variable from a class or if I return a parameter passed into the function. I would return a pointer mostly only if I need to return a member variable that is a pointer or I pass in a parameter that is a pointer. That is if I only need what the pointer points to, of course. Otherwise I might need to return a reference to said pointer... but that has rarely, if ever, happened. I can't recall any instance returning a reference to a pointer. Doesn't mean they haven't happened, though. It just means they're rare.
    Last edited by Elysia; 01-24-2014 at 11:45 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 06-10-2013, 04:03 PM
  2. function not returning correct values
    By Paul Adams in forum C Programming
    Replies: 2
    Last Post: 02-24-2012, 09:58 PM
  3. Having a function return three values
    By alanb in forum C++ Programming
    Replies: 12
    Last Post: 09-02-2009, 08:51 AM
  4. function return values
    By ashok449 in forum C Programming
    Replies: 10
    Last Post: 12-21-2007, 02:57 AM
  5. can a function return 2 values?
    By panfilero in forum C Programming
    Replies: 2
    Last Post: 09-27-2005, 02:35 AM