function return multi value to main function
up vote
1
down vote
favorite
This code is a part of my main code , in this part, each product code that user enter , the quantity and total price will increase, at the end, the user return totalSum, quantity1,quantity2,quantity3,quantity4 to main function,then proceed to print receipt , I know to return to main function have to do like this z= getTotalSum(). but how for multi return? (currently I put as 0 because I don't know how to put)
#include <stdio.h>
int getTotalSum (void); // when user selected product will calculate sum,quantity of each product
int main ()
{
getTotalSum();
return 0;
}
int getTotalSum (void)
{
int code;
float sum=0,totalSum,quantity1=0,quantity2=0,quantity3=0,quantity4=0;
while(1)
{
printf("Enter a product code.(Enter 5 to get total sum)n");
scanf("%d",&code);
switch(code)
{
case 1:
{
quantity1=( quantity1 + 1);
sum=( 45.20 + sum);
printf("Current Sum: RM%.2fn",sum);
break;
}
case 2:
{
quantity2=( quantity2 + 1);
sum=(14.50 + sum);
printf("Current Sum: RM%.2fn",sum);
break;
}
case 3:
{
quantity3=( quantity3 + 1);
sum=(3.45 + sum);
printf("Current Sum: RM%.2fn",sum);
break;
}
case 4:
{
quantity4=( quantity4 + 1);
sum=(7.80 + sum);
printf("Current Sum: RM%.2fn",sum);
break;
}
case 5:
totalSum= sum;
printf("+--------------+--------------------+----------+n");
printf("| Product Code | Rentail Price (RM) | Quantity |n");
printf("+--------------+--------------------+----------+n");
printf("| 1 | 45.20 | %.f |n",quantity1);
printf("+--------------+--------------------+----------+n");
printf("| 2 | 14.50 | %.f |n",quantity2);
printf("+--------------+--------------------+----------+n");
printf("| 3 | 3.45 | %.f |n",quantity3);
printf("+--------------+--------------------+----------+n");
printf("| 4 | 7.80 | %.f |n",quantity4);
printf("+--------------+--------------------+----------+n");
printf("Total Sum: RM%.2fn",totalSum);
return 0;
}
}
}
c return
|
show 14 more comments
up vote
1
down vote
favorite
This code is a part of my main code , in this part, each product code that user enter , the quantity and total price will increase, at the end, the user return totalSum, quantity1,quantity2,quantity3,quantity4 to main function,then proceed to print receipt , I know to return to main function have to do like this z= getTotalSum(). but how for multi return? (currently I put as 0 because I don't know how to put)
#include <stdio.h>
int getTotalSum (void); // when user selected product will calculate sum,quantity of each product
int main ()
{
getTotalSum();
return 0;
}
int getTotalSum (void)
{
int code;
float sum=0,totalSum,quantity1=0,quantity2=0,quantity3=0,quantity4=0;
while(1)
{
printf("Enter a product code.(Enter 5 to get total sum)n");
scanf("%d",&code);
switch(code)
{
case 1:
{
quantity1=( quantity1 + 1);
sum=( 45.20 + sum);
printf("Current Sum: RM%.2fn",sum);
break;
}
case 2:
{
quantity2=( quantity2 + 1);
sum=(14.50 + sum);
printf("Current Sum: RM%.2fn",sum);
break;
}
case 3:
{
quantity3=( quantity3 + 1);
sum=(3.45 + sum);
printf("Current Sum: RM%.2fn",sum);
break;
}
case 4:
{
quantity4=( quantity4 + 1);
sum=(7.80 + sum);
printf("Current Sum: RM%.2fn",sum);
break;
}
case 5:
totalSum= sum;
printf("+--------------+--------------------+----------+n");
printf("| Product Code | Rentail Price (RM) | Quantity |n");
printf("+--------------+--------------------+----------+n");
printf("| 1 | 45.20 | %.f |n",quantity1);
printf("+--------------+--------------------+----------+n");
printf("| 2 | 14.50 | %.f |n",quantity2);
printf("+--------------+--------------------+----------+n");
printf("| 3 | 3.45 | %.f |n",quantity3);
printf("+--------------+--------------------+----------+n");
printf("| 4 | 7.80 | %.f |n",quantity4);
printf("+--------------+--------------------+----------+n");
printf("Total Sum: RM%.2fn",totalSum);
return 0;
}
}
}
c return
2
You can't return multiple values, but you can send multiple variables by reference and change them in the function.
– Deanie
Nov 9 at 16:17
2
Alternatively, you may bundle your return values in astructand then you can return them from function as one return value.
– Scheff
Nov 9 at 16:17
What are the "multi" values you want to return? Please provide an example.
– chux
Nov 9 at 16:18
@chux totalSum, quantity1,quantity2,quantity3,quantity4
– Scheff
Nov 9 at 16:18
@Scheff thank you for suggestion , can you show me how to do it because I'm learning to code , and your suggestion sounds like a new thing to me.
– zhezhi chew
Nov 9 at 16:23
|
show 14 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
This code is a part of my main code , in this part, each product code that user enter , the quantity and total price will increase, at the end, the user return totalSum, quantity1,quantity2,quantity3,quantity4 to main function,then proceed to print receipt , I know to return to main function have to do like this z= getTotalSum(). but how for multi return? (currently I put as 0 because I don't know how to put)
#include <stdio.h>
int getTotalSum (void); // when user selected product will calculate sum,quantity of each product
int main ()
{
getTotalSum();
return 0;
}
int getTotalSum (void)
{
int code;
float sum=0,totalSum,quantity1=0,quantity2=0,quantity3=0,quantity4=0;
while(1)
{
printf("Enter a product code.(Enter 5 to get total sum)n");
scanf("%d",&code);
switch(code)
{
case 1:
{
quantity1=( quantity1 + 1);
sum=( 45.20 + sum);
printf("Current Sum: RM%.2fn",sum);
break;
}
case 2:
{
quantity2=( quantity2 + 1);
sum=(14.50 + sum);
printf("Current Sum: RM%.2fn",sum);
break;
}
case 3:
{
quantity3=( quantity3 + 1);
sum=(3.45 + sum);
printf("Current Sum: RM%.2fn",sum);
break;
}
case 4:
{
quantity4=( quantity4 + 1);
sum=(7.80 + sum);
printf("Current Sum: RM%.2fn",sum);
break;
}
case 5:
totalSum= sum;
printf("+--------------+--------------------+----------+n");
printf("| Product Code | Rentail Price (RM) | Quantity |n");
printf("+--------------+--------------------+----------+n");
printf("| 1 | 45.20 | %.f |n",quantity1);
printf("+--------------+--------------------+----------+n");
printf("| 2 | 14.50 | %.f |n",quantity2);
printf("+--------------+--------------------+----------+n");
printf("| 3 | 3.45 | %.f |n",quantity3);
printf("+--------------+--------------------+----------+n");
printf("| 4 | 7.80 | %.f |n",quantity4);
printf("+--------------+--------------------+----------+n");
printf("Total Sum: RM%.2fn",totalSum);
return 0;
}
}
}
c return
This code is a part of my main code , in this part, each product code that user enter , the quantity and total price will increase, at the end, the user return totalSum, quantity1,quantity2,quantity3,quantity4 to main function,then proceed to print receipt , I know to return to main function have to do like this z= getTotalSum(). but how for multi return? (currently I put as 0 because I don't know how to put)
#include <stdio.h>
int getTotalSum (void); // when user selected product will calculate sum,quantity of each product
int main ()
{
getTotalSum();
return 0;
}
int getTotalSum (void)
{
int code;
float sum=0,totalSum,quantity1=0,quantity2=0,quantity3=0,quantity4=0;
while(1)
{
printf("Enter a product code.(Enter 5 to get total sum)n");
scanf("%d",&code);
switch(code)
{
case 1:
{
quantity1=( quantity1 + 1);
sum=( 45.20 + sum);
printf("Current Sum: RM%.2fn",sum);
break;
}
case 2:
{
quantity2=( quantity2 + 1);
sum=(14.50 + sum);
printf("Current Sum: RM%.2fn",sum);
break;
}
case 3:
{
quantity3=( quantity3 + 1);
sum=(3.45 + sum);
printf("Current Sum: RM%.2fn",sum);
break;
}
case 4:
{
quantity4=( quantity4 + 1);
sum=(7.80 + sum);
printf("Current Sum: RM%.2fn",sum);
break;
}
case 5:
totalSum= sum;
printf("+--------------+--------------------+----------+n");
printf("| Product Code | Rentail Price (RM) | Quantity |n");
printf("+--------------+--------------------+----------+n");
printf("| 1 | 45.20 | %.f |n",quantity1);
printf("+--------------+--------------------+----------+n");
printf("| 2 | 14.50 | %.f |n",quantity2);
printf("+--------------+--------------------+----------+n");
printf("| 3 | 3.45 | %.f |n",quantity3);
printf("+--------------+--------------------+----------+n");
printf("| 4 | 7.80 | %.f |n",quantity4);
printf("+--------------+--------------------+----------+n");
printf("Total Sum: RM%.2fn",totalSum);
return 0;
}
}
}
c return
c return
asked Nov 9 at 16:13
zhezhi chew
105
105
2
You can't return multiple values, but you can send multiple variables by reference and change them in the function.
– Deanie
Nov 9 at 16:17
2
Alternatively, you may bundle your return values in astructand then you can return them from function as one return value.
– Scheff
Nov 9 at 16:17
What are the "multi" values you want to return? Please provide an example.
– chux
Nov 9 at 16:18
@chux totalSum, quantity1,quantity2,quantity3,quantity4
– Scheff
Nov 9 at 16:18
@Scheff thank you for suggestion , can you show me how to do it because I'm learning to code , and your suggestion sounds like a new thing to me.
– zhezhi chew
Nov 9 at 16:23
|
show 14 more comments
2
You can't return multiple values, but you can send multiple variables by reference and change them in the function.
– Deanie
Nov 9 at 16:17
2
Alternatively, you may bundle your return values in astructand then you can return them from function as one return value.
– Scheff
Nov 9 at 16:17
What are the "multi" values you want to return? Please provide an example.
– chux
Nov 9 at 16:18
@chux totalSum, quantity1,quantity2,quantity3,quantity4
– Scheff
Nov 9 at 16:18
@Scheff thank you for suggestion , can you show me how to do it because I'm learning to code , and your suggestion sounds like a new thing to me.
– zhezhi chew
Nov 9 at 16:23
2
2
You can't return multiple values, but you can send multiple variables by reference and change them in the function.
– Deanie
Nov 9 at 16:17
You can't return multiple values, but you can send multiple variables by reference and change them in the function.
– Deanie
Nov 9 at 16:17
2
2
Alternatively, you may bundle your return values in a
struct and then you can return them from function as one return value.– Scheff
Nov 9 at 16:17
Alternatively, you may bundle your return values in a
struct and then you can return them from function as one return value.– Scheff
Nov 9 at 16:17
What are the "multi" values you want to return? Please provide an example.
– chux
Nov 9 at 16:18
What are the "multi" values you want to return? Please provide an example.
– chux
Nov 9 at 16:18
@chux totalSum, quantity1,quantity2,quantity3,quantity4
– Scheff
Nov 9 at 16:18
@chux totalSum, quantity1,quantity2,quantity3,quantity4
– Scheff
Nov 9 at 16:18
@Scheff thank you for suggestion , can you show me how to do it because I'm learning to code , and your suggestion sounds like a new thing to me.
– zhezhi chew
Nov 9 at 16:23
@Scheff thank you for suggestion , can you show me how to do it because I'm learning to code , and your suggestion sounds like a new thing to me.
– zhezhi chew
Nov 9 at 16:23
|
show 14 more comments
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
There are several methods you may employ to return multiple values:
returnastruct
- Write to several variables passed to function by address
- Write to
structpassed to function by address - Write to an array passed to function
An example of each follows:
1
struct data
{
int one;
int two;
int three;
int four;
};
struct data getTotalSum(void)
{
...
}
2
void getTotalSum(int *one, int *two, int *three, int *four)
{
...
}
3
struct data
{
int one;
int two;
int three;
int four;
};
void getTotalSum(struct data *d)
{
...
}
4
void getTotalSum(int *array)
{
...
}
Solely that you created instances (named 'data') of anonymous structs (or forgot thetypedef)...
– Aconcagua
Nov 9 at 16:31
@Aconcagua Forgot the syntax. Danke schön.
– Fiddling Bits
Nov 9 at 16:33
1
You could also write to a variable in the global space.
– Jason Brown
Nov 9 at 17:08
3
@JasonBrown On one hand, you are completely right. On the other hand, it might be worth not to encourage this as it's in most cases bad style. May be, it's even better to make it a secret...
– Scheff
Nov 9 at 17:18
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
There are several methods you may employ to return multiple values:
returnastruct
- Write to several variables passed to function by address
- Write to
structpassed to function by address - Write to an array passed to function
An example of each follows:
1
struct data
{
int one;
int two;
int three;
int four;
};
struct data getTotalSum(void)
{
...
}
2
void getTotalSum(int *one, int *two, int *three, int *four)
{
...
}
3
struct data
{
int one;
int two;
int three;
int four;
};
void getTotalSum(struct data *d)
{
...
}
4
void getTotalSum(int *array)
{
...
}
Solely that you created instances (named 'data') of anonymous structs (or forgot thetypedef)...
– Aconcagua
Nov 9 at 16:31
@Aconcagua Forgot the syntax. Danke schön.
– Fiddling Bits
Nov 9 at 16:33
1
You could also write to a variable in the global space.
– Jason Brown
Nov 9 at 17:08
3
@JasonBrown On one hand, you are completely right. On the other hand, it might be worth not to encourage this as it's in most cases bad style. May be, it's even better to make it a secret...
– Scheff
Nov 9 at 17:18
add a comment |
up vote
3
down vote
accepted
There are several methods you may employ to return multiple values:
returnastruct
- Write to several variables passed to function by address
- Write to
structpassed to function by address - Write to an array passed to function
An example of each follows:
1
struct data
{
int one;
int two;
int three;
int four;
};
struct data getTotalSum(void)
{
...
}
2
void getTotalSum(int *one, int *two, int *three, int *four)
{
...
}
3
struct data
{
int one;
int two;
int three;
int four;
};
void getTotalSum(struct data *d)
{
...
}
4
void getTotalSum(int *array)
{
...
}
Solely that you created instances (named 'data') of anonymous structs (or forgot thetypedef)...
– Aconcagua
Nov 9 at 16:31
@Aconcagua Forgot the syntax. Danke schön.
– Fiddling Bits
Nov 9 at 16:33
1
You could also write to a variable in the global space.
– Jason Brown
Nov 9 at 17:08
3
@JasonBrown On one hand, you are completely right. On the other hand, it might be worth not to encourage this as it's in most cases bad style. May be, it's even better to make it a secret...
– Scheff
Nov 9 at 17:18
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
There are several methods you may employ to return multiple values:
returnastruct
- Write to several variables passed to function by address
- Write to
structpassed to function by address - Write to an array passed to function
An example of each follows:
1
struct data
{
int one;
int two;
int three;
int four;
};
struct data getTotalSum(void)
{
...
}
2
void getTotalSum(int *one, int *two, int *three, int *four)
{
...
}
3
struct data
{
int one;
int two;
int three;
int four;
};
void getTotalSum(struct data *d)
{
...
}
4
void getTotalSum(int *array)
{
...
}
There are several methods you may employ to return multiple values:
returnastruct
- Write to several variables passed to function by address
- Write to
structpassed to function by address - Write to an array passed to function
An example of each follows:
1
struct data
{
int one;
int two;
int three;
int four;
};
struct data getTotalSum(void)
{
...
}
2
void getTotalSum(int *one, int *two, int *three, int *four)
{
...
}
3
struct data
{
int one;
int two;
int three;
int four;
};
void getTotalSum(struct data *d)
{
...
}
4
void getTotalSum(int *array)
{
...
}
edited Nov 9 at 16:33
answered Nov 9 at 16:19
Fiddling Bits
7,11021938
7,11021938
Solely that you created instances (named 'data') of anonymous structs (or forgot thetypedef)...
– Aconcagua
Nov 9 at 16:31
@Aconcagua Forgot the syntax. Danke schön.
– Fiddling Bits
Nov 9 at 16:33
1
You could also write to a variable in the global space.
– Jason Brown
Nov 9 at 17:08
3
@JasonBrown On one hand, you are completely right. On the other hand, it might be worth not to encourage this as it's in most cases bad style. May be, it's even better to make it a secret...
– Scheff
Nov 9 at 17:18
add a comment |
Solely that you created instances (named 'data') of anonymous structs (or forgot thetypedef)...
– Aconcagua
Nov 9 at 16:31
@Aconcagua Forgot the syntax. Danke schön.
– Fiddling Bits
Nov 9 at 16:33
1
You could also write to a variable in the global space.
– Jason Brown
Nov 9 at 17:08
3
@JasonBrown On one hand, you are completely right. On the other hand, it might be worth not to encourage this as it's in most cases bad style. May be, it's even better to make it a secret...
– Scheff
Nov 9 at 17:18
Solely that you created instances (named 'data') of anonymous structs (or forgot the
typedef)...– Aconcagua
Nov 9 at 16:31
Solely that you created instances (named 'data') of anonymous structs (or forgot the
typedef)...– Aconcagua
Nov 9 at 16:31
@Aconcagua Forgot the syntax. Danke schön.
– Fiddling Bits
Nov 9 at 16:33
@Aconcagua Forgot the syntax. Danke schön.
– Fiddling Bits
Nov 9 at 16:33
1
1
You could also write to a variable in the global space.
– Jason Brown
Nov 9 at 17:08
You could also write to a variable in the global space.
– Jason Brown
Nov 9 at 17:08
3
3
@JasonBrown On one hand, you are completely right. On the other hand, it might be worth not to encourage this as it's in most cases bad style. May be, it's even better to make it a secret...
– Scheff
Nov 9 at 17:18
@JasonBrown On one hand, you are completely right. On the other hand, it might be worth not to encourage this as it's in most cases bad style. May be, it's even better to make it a secret...
– Scheff
Nov 9 at 17:18
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53229422%2ffunction-return-multi-value-to-main-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
You can't return multiple values, but you can send multiple variables by reference and change them in the function.
– Deanie
Nov 9 at 16:17
2
Alternatively, you may bundle your return values in a
structand then you can return them from function as one return value.– Scheff
Nov 9 at 16:17
What are the "multi" values you want to return? Please provide an example.
– chux
Nov 9 at 16:18
@chux totalSum, quantity1,quantity2,quantity3,quantity4
– Scheff
Nov 9 at 16:18
@Scheff thank you for suggestion , can you show me how to do it because I'm learning to code , and your suggestion sounds like a new thing to me.
– zhezhi chew
Nov 9 at 16:23