# Thread: Question on multiple return values

1. ## 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 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");

//printf("Month::Old Balance::Payment::Interest::Principal::New Balance");

getchar();
getchar();
return 0;
}

{
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':
break;
case 'e':
break;
default:
printf("You entered a value outside of A to D.\nPlease only use a, b, c, d or e\n\n");
}
}

}```

2. Sure, only one return value will end up being returned though. You will effectively leave the function at the return statement.

3. Originally Posted by Subsonics
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. You can store those values into an array and when you want to leave, return that array

5. 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.

6. Originally Posted by C_ntua
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.

Originally Posted by Subsonics
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. Originally Posted by NinjaFish
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. Originally Posted by Subsonics
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. 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. 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. 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. Originally Posted by CommonTater
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. Originally Posted by NinjaFish
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.

14. Originally Posted by Steve A.
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. 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 */ };
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.