The ones who are crazy enough to think they can change the world are the ones who do.- Steve Jobs
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.
Lets code and have some fun
#include <stdio.h> int add(int, int) ; //function declaration int main() { //structures declartion struct addition{ 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 arguments to function printf("\nThe sum of two value are : "); printf("%d ", sum.c); return 0; } //Function definition int add(int x, int y) { int sum1; sum1 = x + y; return(sum1); }
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.
Lets code and have some fun
#include <stdio.h> //structures declaration typedef struct { int a, b; int c; }sum; void add(sum) ; //function declaration with struct type sum int main() { sum s1; printf("Enter the value of a : "); scanf("%d",&s1.a); printf("\nEnter the value of b : "); scanf("%d",&s1.b); add(s1); //passing entire structure as an argument to function return 0; } //Function Definition void add(sum s) { int sum1; sum1 = s.a + s.b; printf("\nThe sum of two values are :%d ", sum1); }
The program uses the keyword typedef to create the structure type sum. The variable "s1" is declared as structure type sum.
The Call by value method is inefficient to pass the large structures to functions. C provides the efficient method, known as call by reference to pass large structures to functions. Call by reference method is achieved by passing pointers to functions. Pointer is a variable, used to hold or store the address of another variable.
Lets code and have some fun
#include <stdio.h> //Structure declartion struct employee { char name[40]; int empid; int experience; }emp; void displaydetails(struct employee*); //function declaration int main() { struct employee *empptr; //pointer declaration empptr = &emp; //initial printf("\nEnter the name of the Employee : "); scanf("%s", empptr->name); printf("\nEnter the Employee Id : "); scanf("%d",&empptr->empid); printf("\nEnter Experience of the Employee : "); scanf("%d",&empptr->experience); displaydetails(empptr); return 0; } //Function Definition void displaydetails(struct employee *empptr) { printf("\n---------Details List--------- \n "); printf("Employee Name : %s",empptr->name); printf("\nEmployee ID : %d ",empptr->empid); printf("\nEmployee Experience : %d ",empptr->experience); }
The structure employee has the variable "emp". The pointer variable is declared inside the main function and initialized as &emp. Now the pointer empptr hold the address of structure. The employee structure members can be accessed by using the "empptr->membername". The calling function displaydetails has a pointer "empptr" as an argument. Finally the function will display the details of employee.
We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.
© Copyright 2019