Friday, March 31, 2017

How to use out parameter in C

1. http://student.kfupm.edu.sa/s200372670/Function%20Output%20Parameters%20Implemented%20with%20Pointers(OR%20Function%20with%20Output%20Parameter).htm

Example (i) :
Consider the example of a function that accepts the radius of a circle from the main program and returns the area and circumference of a circle by using the output parameters:

#include <stdio.h>
void area_circum (float radius, float *area, float * circum) ; /* function prototype */
void main (void)
{
float r, a, c ;

printf (“Enter the radius of the circle \n”) ;
scanf (“%f”, &r) ;

area_circum (r, &a, &c) ; /* function call that passes the address of the variables a and c */

printf (“The area is %f and circumference is %f\n”, a, c) ; /* output results */
} //end of main


void area_circum (float radius, float *area, float * circum) // function
{
*area = 3.14 * radius * radius ; /* indirect reference to variable a of main program */
*circum = 2 * 3.14 * radius ; /* indirect reference to variable c of main program */
} // end of function area_circum()

Here the variables area of function area_circum and a of main both refer to the same memory location. Similarly, the variables circum of function area_circum and c of main both refer to the same memory location.

Note that when the function is called in the main program, the output parameters have & attached to them before their name. When the output parameters are used in the function, they are used with * attached to them before their name.

No comments:

Post a Comment

Qualcomm Short Term

  113 is the SL. 1st Target by mid July.

Total Pageviews