Thread: Programming style: use return value of function ? or pass pointer

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    4

    Programming style: use return value of function ? or pass pointer

    Programming style: use return value of function ? or pass pointer as an argument ? (assuming that there is only one output)

    Both should work in terms of functionality, but suppose that it is used very heavily. And also in terms of reliability or security perspective, could anyone give insight to this question ?

    Thank you

    PS. In case function returns a value, where is that value stored in the stack ?
    Will that be overwritten when other variables are written to that original place ?

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    For one output I'd use return value, mostly because I like to keep arguments to a minimum.

    In case function returns a value, where is that value stored in the stack ?
    It isn't. It is copied (a la the operator= function) to the variable of your choosing.
    Consider this post signed

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by shong9 View Post
    PS. In case function returns a value, where is that value stored in the stack ?
    Will that be overwritten when other variables are written to that original place ?
    The manner in which the value is returned is unspecified by the language standard. In practice, simple types are returned in a register. Objects which are larger than a register are returned in a more complicated way which is up to the compiler.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There is no best. It depends on your design.
    Btw, you should pass using reference, not pointer.
    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.

  5. #5
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    Quote Originally Posted by Elysia View Post
    There is no best. It depends on your design.
    Btw, you should pass using reference, not pointer.

    I know this is the C++ board but in the case of C, you would have no other choice.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But since this is C++, we can get away with it. I don't see your point.
    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.

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    It depends on what the function does, what operations are involved, and what error handling is necessary.

    A function operating on native types (any operations that do not generate errors) can probably just return instances of those types (values).

    A function counting matches with a predicate may impose requirements (no errors) on the operations involved.

    A function likely to fail during normal execution should probably not mutate parameters unless absolutely necessary.



    Use what is appropriate for the situation.

    If you flatly standardize on either method, you'll eventually wish you hadn't.

    Soma

    Code:
    // returns the sum of the parameters
    int add(int, int);
    Code:
    // returns the number of matches against the predicate in the data 
    int matches(const std::vector<int> &, bool (*)(int));
    Code:
    // returns false on failure implying an unchanged parameter
    bool loadfromfile(std::map<string, string> &, std::istream &);

  8. #8
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    Quote Originally Posted by Elysia View Post
    But since this is C++, we can get away with it. I don't see your point.
    Thats nice, and you may say things that I see no point to either. Nothing like some punk ass trying to flame. I was just letting him/her know. He/she may not have known thank you very much.

    Now on topic: Shong9, do what makes you happy.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by phantomotap View Post
    It depends on what the function does, what operations are involved, and what error handling is necessary.

    A function operating on native types (any operations that do not generate errors) can probably just return instances of those types (values).

    A function counting matches with a predicate may impose requirements (no errors) on the operations involved.

    A function likely to fail during normal execution should probably not mutate parameters unless absolutely necessary.
    That's an interesting and informative set of criteria -- thanks for posting it. As usual, the right thing to do depends on the situation, and this is only learned by experience (or being exposed to it by people who have such experience).

    In my experience, good designs tend to be created by good designers, and this is a very ephemeral thing to define. It has partially to do with intelligence, a great deal to do with experience, and somewhat to do with luck. In any case, simply asking the question is a sign that you're willing to learn (painfully, in some cases) how to do it right.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Syscal View Post
    Thats nice, and you may say things that I see no point to either. Nothing like some punk ass trying to flame. I was just letting him/her know. He/she may not have known thank you very much.

    Now on topic: Shong9, do what makes you happy.
    Then mention that when you make your reply. Otherwise it is seen as a pointless statement.
    Yet, even so, C++ programmers do not need to understand C concepts and vice versa, unless that is, they are interested in knowing both things.
    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.

  11. #11
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    Heh, when I started learning C, I was sitting there trying to make passing by reference work. I couldn't and finally asked an experienced friend and then he told me there is no such thing as pass by ref in C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM