Return Structure through functions

“Returning structure through function” statement has 2 meanings: Returning structure variable through function, Returning structure member through function. We are going to understand both one by one.

Returning structure variable through function

Syntax of returning structure variable through function is as listed below for function declaration, call, and definition. 

Function declaration
struct  <structure name>  <function name>(<arguments>);
Function call
<function name>(<arguments>); 

//to hold the value which is to be returned from this function, one variable of type structure(which we are returning) should be present to hold returned structure variable
Function definition
struct  <structure name>  <function name>(<arguments >)

{

//code for function

}
Example program of returning structure variable through function.

In the below example program, there are two structures named str and STR. str is having members a and b. ‘a’ is having value 1 and ‘b’ is having value c. STR has members A and B. The function “fun” is copying the value of a to A and b to B.str1 is the structure variable of str and STR1 is the structure variable of STR.In the function argument, we have to write data type.
We want to pass members ‘a’ and ‘b’. So here we have to write data types of ‘a’ and ‘b’ as we want to copy them to another structure. In fun, we have made a local structure variable “STR1” (which is of data type “struct STR”) and returned it, so the returned structure is stored in STR1 as it is holding return values.

#include<stdio.h>
#include<conio.h>
struct str{		
int a;
char b;
}str1={1,'c'};
struct STR{
    int A;
    char B;
}STR1;

struct STR fun(int,char); 

void main()
{
  clrscr();
  STR1=fun(str1.a,str1.b);
  printf("str1 : a = %d , b = %c \nSTR1 : A = %d , B = %c",str1.a,str1.b,STR1.A,STR1.B);
  getch();
}

struct STR fun(int a,char b)
{
    struct STR STR1;
    STR1.A=a;
    STR1.B=b;
    return STR1;
}

Output:

Syntax of returning structure member through function is as listed below for function declaration, call and definition. 

Function declaration
<structure member return type>  <function name>(<arguments>);
Function call
<function name>(<arguments>);

//This will return value of that data type which we want to return from structure
Function definition
< structure member return type>  <function name>(< arguments>);

{

//code for function

}
Example program of passing structure member through function

In the below example program, there is a structure named str and its variable is str1. This structure has members: ‘a’ of int type and ‘b’ of char type. There is a function fun1 that returns the value of ‘a’, which is of int data type. And function fun2 returns the value of ‘b’, that is of char data type.

To hold both values, we have made i to store int returned value and j for char. So at the end of the program, i has the value of a and j has the value of b. And both values are printed.

#include<stdio.h>
#include<conio.h>
struct str{		
int a;
char b;
}str1={1,'c'};
int fun1(void); 
char fun2(void); 
void main()
{
    int i;
    char j;
clrscr();
i=fun1();
j=fun2();
printf("str1 : a = %d , b = %c ",i,j);
getch();
}
int fun1(void)
{
    return str1.a;
}
char fun2(void)
{
    return str1.b;
}

Output:

Learning from this blog:

  1. Difference between “Returning structure variable through function” and “Returning structure member through function”.
  2. Syntax of function declaration/ call/ definition for “Returning structure variable through function”.
  3. Syntax of function declaration/ call/ definition for “Returning structure member through function”.
  4. How to write code for both.
  5. Need of using both “Returning structure variable through function” and “Returning structure member through function”.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments