Funcions-can they return more than 1 value?

This is a discussion on Funcions-can they return more than 1 value? within the C++ Programming forums, part of the General Programming Boards category; Hi, Just practising making my own header files and functions.I have created a function that generates 4 different varibles.I have ...

  1. #1
    neopyhte Labmouse's Avatar
    Join Date
    Aug 2007
    Location
    Ireland
    Posts
    26

    Funcions-can they return more than 1 value?

    Hi,

    Just practising making my own header files and functions.I have created a function that generates 4 different varibles.I have set the function to return all 4.It compiles fine but when i call the function in main and then try and cout the 4 values the programme just closes without any out put.

    Have tried to include the 'couting'(if you get my meaning) of the value's within the function,but the compiler starts showing an error(cout-undeclared identifier).
    Any ideas?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    Pass pointers (or better, references) to where you want the additional answers stored.

    Or create a struct containing all 4 values, then return that struct by value (not so good if the contained data is on the large side).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    but the compiler starts showing an error(cout-undeclared identifier)
    did you include iostream?
    did you tell the compiler it's part of the std namespace?

    can they return more than 1 value?
    they can be returned in the form of an array, struct or class

    the programme just closes without any out put
    did you tell it to wait after it couts the 4 variables?

    it'd be easier for us if you showed us some code

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  4. #4
    neopyhte Labmouse's Avatar
    Join Date
    Aug 2007
    Location
    Ireland
    Posts
    26
    only started C++ recently and am only reading up on pointers at the moment so that's beyond me at the oment but thanks anyway

    Labmouse

  5. #5
    neopyhte Labmouse's Avatar
    Join Date
    Aug 2007
    Location
    Ireland
    Posts
    26
    Didnt include <iostream> in the header file but have in main().And also used cin.get() after the 4 couts in main.And also using namespace std in main.

  6. #6
    neopyhte Labmouse's Avatar
    Join Date
    Aug 2007
    Location
    Ireland
    Posts
    26
    heres my header file

    Code:
    #ifndef myfunctions_h
    #define myfunctions_h
    
    
    
    int randomNumbers()
    { 
    	
                            int a=(rand()&#37;24)+1;
    			int b=(rand()%24)+1;
    			int c=(rand()%24)+1;
    			int d=(rand()%24)+1;
                
    
    			return a,b,c,d; 
    }
    
    
    
    #endif

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,273
    Quote Originally Posted by Labmouse View Post
    heres my header file
    Bad practice to place function definitions inside header files. You at the very least need to use the "inline" keyword.

    Also, "return a,b,c,d;" just returns the value in d, not all four values. Like other people said you need to return an array or object, or pass the a,b,c,d as reference parameters.

  8. #8
    neopyhte Labmouse's Avatar
    Join Date
    Aug 2007
    Location
    Ireland
    Posts
    26
    Ah ok so just function prototypes in the header file.So it will only return one varible.Haved learnt the use of pointers or structures yet so that path is a no go at the moment

    Thanks all

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You could just make the function return one random number and then call it 4 times.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    References are horrifically easy:
    Code:
    void my_func(int& param1, int& param2, int& param3, int& param4)
    {
    	param1 = 1;
    	param2 = 2;
    	param3 = 3;
    	param4 = 4;
    }
    
    int main()
    {
    	int a,b,c,d;
    	my_func(a, b, c, d);
    	// a = 1, b = 2, c = 3, d = 4
    }
    All you need to do is put a & at the arguments and the function can modify these arguments when you pass variables to it.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by Labmouse View Post
    Code:
    			int a=(rand()%24)+1;
    			int b=(rand()%24)+1;
    			int c=(rand()%24)+1;
    			int d=(rand()%24)+1;
    More than two lines the same = use an array instead.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Personally if I wanted to generate four random numbers I would make a function that generates and returns a number then call it four times instead.

    Alternatively you could make it a bit more flexible by passing an array to the function, then get its size and generate a random number for each element in the array. That way the function would deal with whatever amount of numbers you wanted to generate for it and you wouldn't need a load of parameters. Just an idea.

    As for cout being an undeclared identifier: Are you using standard namespace? I got the same error message everytime I forgot about it.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Quote Originally Posted by h_howee View Post
    can [functions] return more than 1 value?
    they can be returned in the form of an array, struct or class
    Structs or classes, yes. Arrays are tricky to return from functions, though. The simplest way to do it is to make the array static and return a pointer. Or allocate some memory for the array and return that. Or just pass the array into the function as a parameter in the first place . . .

    Any way you look at it, you end up using pointers instead of arrays.
    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.

  14. #14
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    encapsulate them (i.e put them in a struct, array or class) then return them. best and simplest way to do this (although is doesn't seem that easy but it is) is to encapsulate them in a class then define your own cout output operator for that class. Sounds hard but it's really easy (imo anyways)
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  15. #15
    neopyhte Labmouse's Avatar
    Join Date
    Aug 2007
    Location
    Ireland
    Posts
    26

    Lightbulb

    Thanks...in hindsight making a function that returns 1 random number and then calling it 4 times is fairly goddamm obvious!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 11:00 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 help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 02:08 AM
  4. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM

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