Like Tree1Likes

Question on multiple return values

This is a discussion on Question on multiple return values within the C Programming forums, part of the General Programming Boards category; I am wondering if it is possible to do multiple return values from another function. So far i'v not seen ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    62

    Question on multiple return values

    I am wondering if it is possible to do multiple return values from another function. So far i'v not seen any more than one being called (e.g return value; ) at the end of a function being called. It is possible to do multiple returns throughout a function? I'll give an example what I mean. Anything in red isn't part of my code, but just an idea of how it would work.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int menu(void);
    
    
    int main(void)
    {    
         int choice;
         
        printf("--------------------\nEnter in the Alpha-Numeric \nCharacter next to your choice"
                                           "\n--------------------\n\n");
                                           
        printf("||a. Enter the Principal                     ||\n"
                     "||b. Enter the annual interest rate          ||\n"
                     "||c. Enter duration of loan in months        ||\n"
                     "||d. Calculate your loan payments.           ||\n"
                     "||e. Show Loan Table.                        ||\n"
                     "||f. Exit Program.                           ||\n");
    
        choice = menu();
        
        
        //printf("Month::Old Balance::Payment::Interest::Principal::New Balance");
        
        
        getchar();
        getchar();
        return 0;
    }
    
    int menu(void)
    {
        int choice;
        int principal;
        int rate;
        int months;
        
        choice = getchar();
        
      
        while ((choice = getchar()) != 'f')
              {
                  switch (choice)
                  {
                         case 'a':
                              printf("What is the principal you wish to enter?:\n");
                              principal = getchar();
                              return principal;
                              break;
                         case 'b':
                              printf("What is the annual interest rate?:\n");
                              rate = getchar();
                              return rate;
                              break;
                         case 'c':
                              printf("How many months in which does the loan need to be paid?:\n");
                              months = getchar();
                              return months;
                              break;
                         case 'd':
                              printf("Your payment chart is calculated.\n");
                              break;
                         case 'e':
                              printf("Below is your loan table.\n\n\n");
                              break;
                         default:
                                 printf("You entered a value outside of A to D.\nPlease only use a, b, c, d or e\n\n");
                  } 
              }
           
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Sure, only one return value will end up being returned though. You will effectively leave the function at the return statement.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    Quote Originally Posted by Subsonics View Post
    Sure, only one return value will end up being returned though. You will effectively leave the function at the return statement.
    Is there any way to change what i'v got to stay in the menu function and keep returning values without hoping back into main until I want it to?

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    You can store those values into an array and when you want to leave, return that array

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Well no, when you 'return' you leave the function. But you can flesh out your code and do some more work before you return however.
    Last edited by Subsonics; 04-16-2011 at 03:13 PM.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    Quote Originally Posted by C_ntua View Post
    You can store those values into an array and when you want to leave, return that array
    Any way you can explain how to do that? My C Primer plus 5th Edition book doesn't even talk about how to store into arrays unless you're using a for loop to increase and add into it. And I don't really need a loop for that, just how do store into individual slots. I'v tried ways I thought would work, but my program just hangs on 6-7 compiler errors when I try that.


    Quote Originally Posted by Subsonics View Post
    Well no, when you 'return' you leave the function. But you can flesh you your code and do some more work before you return however.
    What are you referring to when you say I can flesh my code to do some more work?

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by NinjaFish View Post
    What are you referring to when you say I can flesh my code to do some more work?
    At the moment you take a number from stdin and return. What I meant was that you can take the number, calculate loan payments and return the answer.

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    Quote Originally Posted by Subsonics View Post
    At the moment you take a number from stdin and return. What I meant was that you can take the number, calculate loan payments and return the answer.
    Well, all the values need to be returned to my main, since that is were the entire calculation needs to be done. The calculation will print out a loan table, which shows how much is paid each month, old balance, new balance, principal etc. And the interest rate calculation is done via that math formula in main.

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    No, it doesn't need to be done in main that is my point, it can however, but it's your program so it's up to you. But, your a,b,c choices only has one value so I don't get the point you made about returning multiple values. Your switch/case will execute only one of these.

  10. #10
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    What I want it to do, is go through the case statement until you decide to quit it. when they hit 'a' they input the principal of the loan. When they hit 'b', they input the rate, and 'c' is for months. Then I want to return those values to main to do the calculation. I was going to do the calculation in the menu function, than put the entire thing to main to be printed, however I have NO idea how to even begin doing something like that.

    So I decided to return the variables to my main function, and than do the calculation and printout of the loan table there.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ninja Fish... the best kept secret in programming is simply this: "It ain't never going to learn our language, so we have to learn it's."

    C can only return a single value and only of the type specified in the function definition. (i.e. char flub() ain't never going to return a double.)


    Combine your menu into main and use functions to do the calculations.

  12. #12
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    Quote Originally Posted by CommonTater View Post
    Ninja Fish... the best kept secret in programming is simply this: "It ain't never going to learn our language, so we have to learn it's."

    C can only return a single value and only of the type specified in the function definition. (i.e. char flub() ain't never going to return a double.)


    Combine your menu into main and use functions to do the calculations.
    I could do this without a function at all, however it's not within the rules of what i'm doing. It HAS to use a menu function to get the variables for each. However, the calculation can be done inside or outside of main, my choice.

  13. #13
    Registered User
    Join Date
    Sep 2010
    Posts
    69
    Quote Originally Posted by NinjaFish View Post
    It HAS to use a menu function to get the variables for each. However, the calculation can be done inside or outside of main, my choice.
    Sure, have each option call a seperate function, which collects and returns it's own value, or, performs its own task.

    Ninjafish, bear in mind that altho it may have been quite some time ago, we have all been there.
    There is no way a function can return more than a single value.
    Now, that value can be a simple data-type or a Structure, (which can contain multiple values), or an Array, (which can also contains multiple values).
    The latter two are more complex data-types. Probably a bit advanced at this point.
    Last edited by Steve A.; 04-16-2011 at 04:49 PM.

  14. #14
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    Quote Originally Posted by Steve A. View Post
    Sure, have each option call a seperate function, which collects and returns it's own value, or, performs its own task.

    Ninjafish, bear in mind that altho it may have been quite some time ago, we have all been there.
    There is no way a function can return more than a single value.
    Now, that value can be a simple data-type or a Structure, (which can contain multiple values), or an Array, (which can also contains multiple values).
    The latter two are more complex data-types. Probably a bit advanced at this point.
    Asides realizing my blunder using getchar() for integers (I'm on 48 hour days, leave me alone lol, fixed it though), I was supposed to learn Arrays, however the book explains it rather poorly for a good 3 chapters.

    I'd be fine using the array, I just need to know how exactly you put a variable into an array. I know how Arrays work. They have multiple sectors in them to hold multiple pieces of data. Int array[4] holds 5 bits of data. However, the book only explains how to add into array using for loops to increment into the next spot of the array. I just need to know how to manually add a value into each part of an array (i.e principal into array[0], rates into array[1] and months into array[2]. Any idea how to do that, if you can (which I assume you can)?

  15. #15
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    I am wondering if it is possible to do multiple return values from another function. So far i'v not seen any more than one being called (e.g return value; ) at the end of a function being called. It is possible to do multiple returns throughout a function? I'll give an example what I mean. Anything in red isn't part of my code, but just an idea of how it would work.
    No you cannot return more than one thing from a function. Neither can you, as other people have put it, return an array. What you do with arrays is pass them in, like this:
    Code:
    int menu (int *myarray, size_t size);
    int myarray[] = { 0, /*principal */, 0 /* term */, 0 /* rate */ };
    menu(myarray, 3);
    Then menu would change the three values and when it returns those values are available in main(). The truth is, the variables never really left main() like they would at other times. You changed the variables through a pointer. You could also use multiple pointers for month, rate, and principle if you wanted.

    But I think menu() has more problems than that.

    Looking here:
    Code:
    switch (choice)
                  {
                         case 'a':
                              printf("What is the principal you wish to enter?:\n");
                              principal = getchar();
                              return principal;
                              break;
                         case 'b':
                              printf("What is the annual interest rate?:\n");
                              rate = getchar();
                              return rate;
                              break;
                         case 'c':
                              printf("How many months in which does the loan need to be paid?:\n");
                              months = getchar();
                              return months;
    I see that you're asking the user for input on his loan, but none of that data is being read exactly. It's highly unlikely that the loan data will fit in the integer that getchar returns. Read here:
    Return Value

    fgetc(), getc() and getchar() return the character read as an unsigned char cast to an int or EOF on end of file or error.
    So unless the amounts are always ridiculously small... you know what, let's just admit you need numbers.

    Additionally, I think menu is responsible for far too much, or is at least poorly named. It should probably call at least one other function to calculate the loans and print them. I hope you're going in that direction and not trying to do everything in one function.

    HTH

    EDIT: You'll have to excuse me, it takes time to write posts, and sometimes things move along without me.
    I just need to know how to manually add a value into each part of an array (i.e principal into array[0], rates into array[1] and months into array[2]. Any idea how to do that, if you can (which I assume you can)?
    You would read input into a normal variable first and then do something like:
    Code:
    array[0] = principal;
    array[1] = rate;
    array[2] = months;
    whiteflags.
    Last edited by whiteflags; 04-16-2011 at 06:35 PM.

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how is it possible to return multiple values?
    By Masterx in forum C++ Programming
    Replies: 5
    Last Post: 05-04-2009, 06:49 PM
  2. How to use return values
    By Furious5k in forum C++ Programming
    Replies: 3
    Last Post: 02-05-2009, 08:07 PM
  3. Return values
    By pritin in forum C++ Programming
    Replies: 9
    Last Post: 03-26-2007, 05:24 PM
  4. Replies: 4
    Last Post: 03-11-2005, 04:45 PM
  5. C++ .Net DLL return values
    By shuesty in forum C++ Programming
    Replies: 15
    Last Post: 01-19-2003, 04:39 PM

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