search
Login

Categories


C Books Guide and List
C++ Books Guide and List
Best Java Books

3.8k questions

4k answers

117 comments

10.7k users

0 votes
512 views
asked Guru 24.6k points 29 51 76

1 Answer

0 votes
A function in C can return only one value. If we want the function to return multiple values, we need to create a structure variable, which has three integer members and return this structure.

Program: Program with a function to return 3 values

#include<stdio.h>

//sample structure which has three integer variables.

struct sample {

int a, b, c;

};

//this is function which returns three values.

struct sample return3val() {

struct sample s1;

s1.a = 10;

s1.b = 20;

s1.c = 30;

//return structure s1, which means return s1.a ,s1.b and s1.c

return s1;

}

int main() {

struct sample accept3val;

//three values returned are accepted by structure accept3val.

accept3val = return3val();

//prints the values

printf(" \n %d", accept3val.a);

printf("\n %d", accept3val.b);

printf(" \n %d", accept3val.c);

return 0;

}

Output:

10

20

30.

Explanation:

In this program, we use C structure to return multiple values from a function. Here we have a structure holding three int variables and a function which returns it. 'return3val' is a function which assigns 10, 20, 30 to its integer variables and returns this structure. In this program, 'accept3val' is a structure used to accept the  values returned by the function. It accepts those values and shows the output.
answered Guru 39.1k points 10 21 32

Related questions

0 votes
1 answer 44 views
0 votes
1 answer 152 views
152 views
Write a program to accept three sides of a triangle and transfer them to a function to compute the area of a triangle.
asked anonymous
0 votes
1 answer 35 views
0 votes
1 answer 288 views
...