Where do Unused function returns go?

This is a discussion on Where do Unused function returns go? within the C++ Programming forums, part of the General Programming Boards category; I don't know how to explain it but sometimes I see functions that retern iterators (algorithms and the like), return ...

  1. #1
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    Where do Unused function returns go?

    I don't know how to explain it but sometimes I see functions that retern iterators (algorithms and the like), return input streams, etc. Sometimes I see the functions called without the return values being used or assigned. For example, a call to a function that returns a reference to a input stream like

    Code:
    istream &read_file(...) {
    ...
    }
    being called
    Code:
    read_file(...)
    without it's istream being used for anything.



    or
    Code:
    iterator transform( iterator start, iterator end, iterator result, UnaryFunction f );
    being calle without it's return iterator being used.

    I'm sure there's a logical reason for doing so that I haven't thought of, but they otherwise return void?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Nobody forces you to use a return value. But, for example, the stream returning allows you to chain calls (the operator << does that) or test the result for truth (while(getline(...))).

    The iterator returned from transform is the new output iterator, which can be pretty important if you want to, e.g. add more to it. If you don't want to do that, then ignore the return value.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Were you looking for a lower-level explanation?

    Try here: http://www.digitalmars.com/ctg/ctgAsm.html

    Near pointers, ints, unsigned ints, longs, and unsigned longs are returned in EAX. Chars are returned in AL; shorts are returned in AX. Far pointers are returned in DX, EAX, where DX contains the segment and EAX contains the offset. long longs are returned in EDX, EAX.
    When C Linkage is in effect, floats are returned in EAX and doubles in EDX, EAX, where EDX contains the most significant 32 bits and EAX contains the least significant 32 bits. When C++ linkage is in effect, the compiler creates a temporary copy of the variable on the stack and returns a pointer to it. Both these techniques are reentrant.

    1-byte structs are returned in AL, 2-byte structs in AX, and 4-byte structs in EAX. With larger structures, the compiler creates a temporary copy of the variable on the stack and returns a (reentrant) pointer to it.

    For 32-bit C++ code, where a struct has no constructors or destructors declared for it, 1-byte structs are returned in AL, 2-byte structs in AX, 4-byte structs in EAX, and 8-byte structs in EDX: EAX.

    Warning: In previous versions of DMC++, small structs without constructors in 32-bit C++ code were passed through a hidden pointer to the return value. The change described above was made for compatibility with Microsoft. Due to this change, if you build part of an application with the current version of DMC++, you need to rebuild all of the application; otherwise, crash bugs could be introduced.
    I think the compiler can return values however it wants, but I think they all generally return values in registers. I believe that if you don't use the return value, the corresponding register is just simply overwritten eventually with other data.
    Last edited by itsme86; 08-08-2006 at 10:40 AM.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    CornedBees suggestion was what I was looking for. It's just that I'm so used to creating my functions so that if they manipulate a value, they are void, and if they create one, they return something, and that return value is used. it wasn't until C++ until I started to see things like in the algorithm class and return values that were optional to use even in manipulating functions.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    And you will soon be creating your own too. This will be particularly common when you create either member functions that return a reference to the object you just changed, or functions that you want to return more than one value.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  4. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  5. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21