You are on page 1of 5

Structure and Functions

MODULE 3
• C allows programmers to pass a single or entire structure information to or from a

function.

• A structure information can be passed as a function arguments. The structure

variable may be passed as a value or reference. The function will return the value

by using the return statement.


Passing Structure Members To Functions

#include <stdio.h> //Function definition


int add(int, int) ; //function declaration int add(int x, int y)
int main() {
{ int sum1;
//structures declartion sum1 = x + y;
struct addition{ return(sum1);
int a, b; }
int c;
}sum;
printf("Enter the value of a : ");
scanf("%d",&sum.a);
printf("\nEnter the value of b : ");
scanf("%d",&sum.b);
sum.c = add(sum. a, sum.b); //passing structure members as Output
arguments to function
printf("\nThe sum of two value are : "); Enter the value of a 10
printf("%d ", sum.c); Enter the value of b 20
return 0; The sum of two values 30
}
Note:

The structure addition have three variables( a, b, c) which are also known as

structure members. The variables are reads through scanf function. The next

statement illustrates that the structure members can be passed to the function

add. The add function defined to perform addition operation between two

values.
#include <stdio.h>
Passing The Entire Structure To Functions
//structures declaration
//Function Definition
typedef struct {
void add(sum s)
int a, b;
{
int c;
int sum1;
}sum;
sum1 = s.a + s.b;
void add(sum) ; //function declaration with struct type sum
printf("\nThe sum of two values are :%d ", sum1);
int main()
}
{
sum s1;
printf("Enter the value of a : ");
scanf("%d",&s1.a);
printf("\nEnter the value of b : ");
scanf("%d",&s1.b);
Output
add(s1); //passing entire structure as an argument to
function
Enter the value of a 10
return 0;
Enter the value of b 20
}
The sum of two values 30

You might also like