Developer community
0 votes
534 views
asked by Guru (24.6k points) 29 51 79 | 534 views

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 by Guru (39.1k points) 10 22 34

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

Categories


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

3.8k questions

4k answers

117 comments

10.7k users

3,751 questions
4,046 answers
117 comments
10,709 users