Return Multiple Values from Function

How to return multiple values in C language function?

Questions by vijay.karumuri

Showing Answers 1 - 4 of 4 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. span style="color: #ff0000;">"

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

  3. scanf("%d""

  4. Area= %f""

  5. Perimeter= %f"

  Was this answer useful?  Yes

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.  */"%d %f %s", &x, &y, z) ==3)

  5. {

  6.   // process x, y, and z

  7. }

  8.  

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