Returning values from functions

This is a discussion on Returning values from functions within the C++ Programming forums, part of the General Programming Boards category; I understand about returning a value from a function but is it possible to return more than 1 value or ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    96

    Returning values from functions

    I understand about returning a value from a function but is it possible to return more than 1 value or do you need to use global variables to manage that.

    I have a function that produces 4 variables, dayname, date, monthname and year from time_t; when I call that function I want to be able to use all those variables in the calling function but it would appear I can only return 1 variable; hence my question above - using global variables they would be available to the calling function as well as the function producing them.

    Or should I take the easy way out and use the called function listing as part of the calling function.

    I hope that makes sense! - I'm sure you'll tell me if it doesn't!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    You can only return 1 "thing" however that thing can be a collection of values as in a struct containing multiple data members. Values passed into the function can also be modified by said function if they are passed as pointers but I personally don't count these values as "returned" by the function. Either of these routes would work for you. Globals should be avoided if at all possible.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    96
    Thanks for that - it sure looks like my next topic to learn should be structures and their use - ho hum wish I could see a light at the end of this tunnel....

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Quote Originally Posted by hk_mp5kpdw View Post
    ...Values passed into the function can also be modified by said function if they are passed as pointers but I personally don't count these values as "returned" by the function...
    Or references. Don't forget references.
    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
    Registered User
    Join Date
    Oct 2006
    Posts
    2,361
    Quote Originally Posted by JayCee++ View Post
    ...wish I could see a light at the end of this tunnel....
    it's more of a virtual light. you have to realize that there is no end to learning programming. in fact, there is no end to learning a language. with that in mind, you have to come to the understanding that the more you know, the more you can do, and perhaps it would be helpful to pick a specific goal to work up to, such as developing an application that you find useful. even try reverse-engineering an existing application that you use every day to duplicate its functionality. learn the things you need to know to make that happen, and then once you achieve that goal, set a new, higher goal, and learn what you need to achieve that one.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning Values in C
    By cuba06 in forum C Programming
    Replies: 6
    Last Post: 11-21-2007, 12:23 AM
  2. Returning values from functions
    By g001 in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2007, 03:42 AM
  3. returning multiple values from functions?
    By jamesn56 in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2005, 01:10 PM
  4. Functions and Returning Values
    By jrahhali in forum C++ Programming
    Replies: 5
    Last Post: 08-22-2004, 12:13 PM
  5. Returning Values
    By incognito in forum C++ Programming
    Replies: 2
    Last Post: 03-17-2002, 08:31 AM

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