C Tutorial
In c programming, function can return a single value at a time, lets see how we can make function to return multiple values through below program.
Using call by reference, we can make function return more than one values at a time which is not possible ordinarily.
Call by reference means passing variable addresses to the function instead of values.
#include<stdio.h> void computecircleparams(int, float*, float*); void main() { int radius; float area, perimeter; printf("\nEnter radius of a circle: "); scanf("%d", &radius); computecircleparams(radius, &area, &perimeter); printf("\nCircle Area: %f", area); printf("\nCircle Perimeter: %f", perimeter); } void computecircleparams(int cradius, float *carea, float *cperimeter) { *carea = 3.14 * cradius * cradius; *cperimeter = 2 * 3.14 * cradius; }
Output:
$ cc multiple-return-values.c $ ./a.out Enter radius of a circle: 10 Circle Area: 314.000000 Circle Perimeter: 62.799999
This c program passing address to a function for float variable area and perimeter.
computecircleparams function computes the area and perimeter using radius passed to this function, and updates the reference of area and perimeter variables with computed value using area and perimeter formula's.
C Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us
| Report website issues in Github
| Facebook page
| Google+ page