Thread: Function Returning Structures

  1. #1
    Aspiring "Software Guy"
    Join Date
    Aug 2005
    Posts
    46

    Function Returning Structures

    I just want to know, how to use the function "sum" effectively, returned in the following code.

    distance is a structure.

    Code:
    distance &sum(distance L1, distanceL2);
    distance L3; // global structure
    
    distance &sum(distance L1,distance L2)
    {
    L3.feet=L1.feet+L2.feet+(L1.inches+L2.inches)/12;
    L3.inches=(L1.inches+L2.inches)%12;
    return L3;
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well . . . sum() takes two distance structures, adds their contents and stores the result in the global variable L3, also returning a reference to L3. You could use it like so:
    Code:
    distance one = {1, 9}, two = {2, 11};
    distance both = sum(one, two);
    In my opinion it's not written very well. First of all, L1, L2, and L3 are very bad variable names. They could be anything. "result", "one" and "two" might be better. Secondly, using a global variable for just that is extremely wasteful. And why return a reference to the variable? It's already global . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Aspiring "Software Guy"
    Join Date
    Aug 2005
    Posts
    46
    #1 : SUPPOSE sum() returned a value, is it right to do

    cout << sum(L1,L2).feet;
    -------------

    #2 How to print values of L3 in the original case, where sum() returns a reference to L3 case?

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by pritin View Post
    #1 : SUPPOSE sum() returned a value, is it right to do
    Code:
     cout << sum(L1,L2).feet;
    You mean, will that work? Sure. This would work too:
    Code:
    sum(L1, L2);
    cout << L3.feet;
    #2 How to print values of L3 in the original case, where sum() returns a reference to L3 case?
    You mean, how would you print the previous value of L3 after sum() has modified it? You can't. That's why I don't like using global variables.

    You'd have to save the value first:
    Code:
    distance prev = L3;
    sum(L1, L2);
    // L3 is the new
    // prev is the old L3
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM