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;
}
}
}









share|improve this question


















  • 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 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












  • @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

















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;
}
}
}









share|improve this question


















  • 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 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












  • @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















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;
}
}
}









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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












  • @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




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












  • @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














1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










There are several methods you may employ to return multiple values:





  1. return a struct

  2. Write to several variables passed to function by address

  3. Write to struct passed to function by address

  4. 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)
{
...
}





share|improve this answer























  • 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








  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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:





  1. return a struct

  2. Write to several variables passed to function by address

  3. Write to struct passed to function by address

  4. 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)
{
...
}





share|improve this answer























  • 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








  • 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















up vote
3
down vote



accepted










There are several methods you may employ to return multiple values:





  1. return a struct

  2. Write to several variables passed to function by address

  3. Write to struct passed to function by address

  4. 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)
{
...
}





share|improve this answer























  • 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








  • 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













up vote
3
down vote



accepted







up vote
3
down vote



accepted






There are several methods you may employ to return multiple values:





  1. return a struct

  2. Write to several variables passed to function by address

  3. Write to struct passed to function by address

  4. 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)
{
...
}





share|improve this answer














There are several methods you may employ to return multiple values:





  1. return a struct

  2. Write to several variables passed to function by address

  3. Write to struct passed to function by address

  4. 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)
{
...
}






share|improve this answer














share|improve this answer



share|improve this answer








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 the typedef)...
    – 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










  • @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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Landwehr

Reims

Schenkenzell