Returning multiple values

This is a discussion on Returning multiple values within the C Programming forums, part of the General Programming Boards category; Hi, are there any alternative ways to return multiple value in only one function without using pointers , global variables ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    82

    Question Returning multiple values

    Hi, are there any alternative ways to return multiple value in only one function without using pointers, global variables, struct and macros?

    *I have done it using pointers, but I want to know if there are any alternative ways to do it*

    My example code using pointer
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void get_print_Sales(); //Get the sales & print it using printSales function
    
    void printSales(int *s, int *t); //Include this in get_print_Sales to print the sales in get_print_Sales function
    
    int main(void)
    {
    
        get_print_Sales();
    
        return 0;
    
    }
    void get_print_Sales()
    {
    
        int sales, tax;
    
        printf("Enter sales amount : ");
        scanf("%d", &sales);
    
        printf("Enter tax amount : ");
        scanf("%d", &tax);
    
        printSales(&sales, &tax);
    
    }
    
    void printSales(int *s, int *t)
    {
    
        printf("Sales amount : %d", *s);
    
        printf("Tax amount : %d", *t);
    
    }
    Last edited by xeon321; 06-19-2012 at 09:18 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,662
    Yes, e.g., if you want to return 8 boolean values, you can just return an unsigned char in which each bit corresponds to a boolean value.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    Umm, anymore other ways? This is due to in my assignment , it has been stated that the program can be done by using first seven chapters in my current syllabus.

    Which seven I can't really tell you as it is quite long and I don't know the technical terms / proper names for them.
    All that I can say is, they are very basics ( those that you learnt when you started to learn programming ) ..
    *ps : I have not learnt bool yet, nor it is in my current syllabus*

    Ermm, most of em are :
    If statements Loops in C Functions and Program Organization Switch case Typecasting

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,554
    Quote Originally Posted by xeon321 View Post
    Hi, are there any alternative ways to return multiple value in only one function without using pointers, global variables, struct and macros?

    *I have done it using pointers, but I want to know if there are any alternative ways to do it*

    My example code using pointer
    I my opinion your function getSales does NOT do what you say you what to do.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    Quote Originally Posted by stahta01 View Post
    I my opinion your function getSales does NOT do what you say you what to do.

    Tim S.
    Sorry, the function name is confusing. It is fixed now, including comments written.

    And for your info, it does what I exactly I want it to do.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,662
    Quote Originally Posted by xeon321
    Umm, anymore other ways? This is due to in my assignment , it has been stated that the program can be done by using first seven chapters in my current syllabus.
    Uh, you're asking the wrong question then. There are two accepted ways to "return multiple values" from a function: either return a struct or use out parameters (which will be pointers). The use of globals to facilitate such a thing is discouraged. The use of a macro does not count. What I talked about is just the return of a single value that is interpreted as multiple values. As such, choose one of the two main ways and be done with it.

    EDIT:
    Well, okay, there is a third way that shares some of the flaws of a global, but I guess it is acceptable because the standard library does it, e.g., strtok: you can return a pointer to an element of a static local array, and hence access the rest of the array. In a sense this is "returning multiple values". However, it shares a problem with the use of globals in that it is not re-entrant.
    Last edited by laserlight; 06-19-2012 at 09:23 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    Hmm, nevermind. I will approach my lecturer to fix this problem. Something's very wrong in the assignment requirement. Thanks for the advice anyway

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,662
    Well, in your program, there is no need for printSales to have pointer parameters. You can pass the integers by value.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    Ah I got you. But hmm, sorry. This is a really bad example. In the actual program, I have lots more other things.
    Last edited by xeon321; 06-19-2012 at 09:34 AM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,662
    That is, you could have written:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* Get the sales & print it using printSales function */
    void get_print_Sales(void);
    
    /* Include this in get_print_Sales to print the sales in get_print_Sales function */
    void printSales(int sales, int tax);
    
    int main(void)
    {
        get_print_Sales();
        return 0;
    }
    
    void get_print_Sales(void)
    {
        int sales, tax;
    
        printf("Enter sales amount : ");
        scanf("%d", &sales);
    
        printf("Enter tax amount : ");
        scanf("%d", &tax);
    
        printSales(sales, tax);
    }
    
    void printSales(int sales, int tax)
    {
        printf("Sales amount : %d", sales);
        printf("Tax amount : %d", tax);
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    Ah I got you. But hmm, sorry. This is a really bad example. In the actual program, I have lots more other things. Like, passing the value to 3rd function and 5th and finally to the main to print it out.

    But thanks for the pointing out, made me realize that I can actually shorten many lines of code!

    *So I guess our lecturer's idea is to make us write many functions and pass down the values like a staircase. *
    Last edited by xeon321; 06-19-2012 at 09:39 AM.

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    2,554
    Would using a array count?

    It really depends on his/her definition of the word pointer and the word return.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  13. #13
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    Nah, it is in Chapter 9 for my class. And my lecturer only reached Chapter 7 ( Loop ).

    Well , pointer = *b.

    Speaking of return, I would like to ask, for example, the value that you want to return is 3. The location where it returns to is only limited to main function? Or it can be anywhere else?

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,662
    Quote Originally Posted by xeon321
    Speaking of return, I would like to ask, for example, the value that you want to return is 3. The location where it returns to is only limited to main function? Or it can be anywhere else?
    The location where it returns to is whatever function that called that function, i.e., the value is returned to the caller.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Jun 2012
    Posts
    82
    Ah! Gotcha. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning Multiple Values from a Function
    By Programmer3922 in forum C Programming
    Replies: 4
    Last Post: 08-02-2008, 09:10 PM
  2. returning multiple values from functions?
    By jamesn56 in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2005, 01:10 PM
  3. Returning Multiple Values
    By StarOrbs in forum C++ Programming
    Replies: 5
    Last Post: 03-09-2005, 12:00 PM
  4. Returning multiple values
    By gcn_zelda in forum C++ Programming
    Replies: 13
    Last Post: 08-02-2003, 05:30 PM
  5. Returning Multiple Values from a Function
    By Learner007 in forum C++ Programming
    Replies: 11
    Last Post: 07-21-2002, 11:03 AM

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