Return Multiple Values from Function

How to return multiple values in C language function?

Questions by vijay.karumuri

Showing Answers 1 - 0 of 0 Answers

garima

  • Oct 6th, 2011
 

multiple values can be returned in C using the concepts of pointers.

  Was this answer useful?  Yes

rinsdominic

  • Dec 28th, 2011
 

Multiple values can be returned by using pointers in c...

Code
  1. int main()

  2. {

  3.   int r;

  4. float a,p;


  5. Enter the radius of a circle: ");

  6. scanf("%d",&r);

  7. funAreaPeri(r,&a,&p);


  8. Area= %f",a);


  9. Perimeter= %f",p);

  10. return 0;

  11. }

  12.  

  13. funAreaPeri(int r,float *a,float *p)

  14. {

  15.   *a=3.14*r*r;

  16.   *p=2*3.14*r;

  17. }

  Was this answer useful?  Yes

jbode

  • Jan 3rd, 2012
 

Strictly speaking, a C function can only *return* a single value; you cant write something like "a,b,c=f();".

However, you can write to multiple function parameters in addition to returning a value (such as the library function scanf() - see code sample).

You can return values of struct type, which have multiple attributes. However, struct types should only be used represent things which *logically* have multiple fields or attributes, such as a street address (number, street name, city, state/province/whatever, postal code, etc.) or a line item in an invoice (part #, quantity, unit price, total price, etc.). You should never create a struct just to package up multiple items that have no logical relationship to each other.

Code
  1. /**

  2.  * Code sample 1: In addition to writing to its parameters, scanf

  3.  * returns the number of successful conversions

  4.  */

  5. int x;

  6. double y;

  7. char z[SOME_SIZE];

  8. ...

  9. if (scanf("%d %f %s", &x, &y, z) ==3)

  10. {

  11.   // process x, y, and z

  12. }

  13.  

Arpita Mishra

  • Aug 4th, 2012
 

Multiple values can be returned from a function by using call by address i.e. pointers intelligently.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions