Is there a way to return entire structures in one function

This is a discussion on Is there a way to return entire structures in one function within the C++ Programming forums, part of the General Programming Boards category; i was looking at the program that i usually work and i have different functions for getting the first number, ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    151

    Question Is there a way to return entire structures in one function

    i was looking at the program that i usually work and i have different functions for getting the first number, the operation, and the other number, and i was wondering is there a way to return an entire structure

    thank you.

    P.S
    and it would be nice if someone could tell me how to direct one function to another. thank you

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Are you familiar with structs or classes? A simple struct could hold the number, operation, other number, etc. You could use the struct just like any other type like int or double, which includes returning it from a function.

    To "direct" one function to another, just call the other function. I might not be understanding what you're asking, though.

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    If you want to use a function to modify something inside a structure, just specify the struct name, followed by the struct member (separated by a (.) period), like this:
    Code:
    int func1() {
      STRUCTNAME.STRUCTMEMBER = 5;
      cout<< STRUCTNAME.STRUCTMEMBER;
    }
    It would be better to use a single function though to modify and/or display all elements in a struct.
    You get the idea, I'm sure. It is fairly easy.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    i mean can you return a structure with different variable types (2 floats and a char)
    Last edited by bijan311; 12-09-2009 at 04:00 PM. Reason: i changed my mind about what i wanted to say

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    The C approach would be

    Code:
    struct Result_t{
      double part1;
      double part2;
      char part3;
    };
    
    Result_t func(double param1, double param2, char param3){
      Result_t result= {param1, param2, param3};
      result.part2 /= 2;
      return result;
    }
    But generally, in C++ you would instead design classes that do a little more than just serve as a way to return multiple bits of data. You would make a class that groups related data together and provides function on what you might do with that data.
    Last edited by King Mir; 12-09-2009 at 04:48 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM

Tags for this Thread


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