Write a Function to display employee detail through function “call by value” and “Call By reference”
Note: Here I am assuming that reader must have Idea of function calling method as I discussed in “Call By Value and Call By Reference”
“Call By Structure Value” means pass the structure value as an Input to the function.
1 |
display_Struct(emp);//Function Call by value |
where emp is a structure.Lets understand through below example –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <stdio.h> #include<string.h> /*structure Declaration*/ typedef struct { unsigned char name[20]; unsigned int empId; double salary; }empInfo; void display_Struct(empInfo ); //Function Prototype declaration int main() { empInfo emp={"John",12345,89999.466}; /*empInfo is the alias name of struct*/ display_Struct(emp); /*Function Call by value */ return 0; } /*function definition to print the detail of employee passed as argument*/ void display_Struct(empInfo empData) { printf("Size of structure empDatais =%lu\n",sizeof(empData)); printf("Value at structure members are :\n"); printf("empData.name=%s\n",empData.name); printf("empData.empId=%u\n",empData.empId); printf("empData.salary=%lf\n",empData.salary); } |
1 2 3 4 5 6 7 |
Output: Size of structure empDatais =32 Value at structure members are : empData.name=John empData.empId=12345 empData.salary=89999.466000 |
As You Can see :
display_Struct is a function with argument “emp” which is an structure variable itself.
display_Struct(emp); is calling it’s definition by passing value of the structure “emp” to print the information of emp on console.
empData is an local structure variable which take a copy of emp structure for further processing and will print by the use of dot(.) operator.
“Call By Structure Reference” means pass the structure address as an Input to the function.
display_Struct(&emp); //Function Call by Reference
where emp is a structure.Lets understand through below example –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <stdio.h> #include<string.h> /*structure Declaration*/ typedef struct { unsigned char name[20]; unsigned int empId; double salary; }empInfo; void display_Struct(empInfo* ); //Function Prototype declaration int main() { empInfo emp={"John",12345,89999.466}; /*empInfo is the alias name of struct*/ display_Struct(&emp); /*Function Call by reference*/ return 0; } /*function definition to print the detail of employee passed as argument*/ void display_Struct(empInfo *empData) { printf("Size of structure empData is =%lu\n",sizeof(empData)); printf("Value at structure members are :\n"); printf("empData->name=%s\n",empData->name); printf("empData->empId=%u\n",empData->empId); printf("empData->salary=%lf\n",empData->salary); } |
1 2 3 4 5 6 7 |
Output: Size of structure empData is =8 Value at structure members are : empData->name=John empData->empId=12345 empData->salary=89999.466000 |
As You Can see :
display_Struct is a function with argument “emp” which is an structure variable itself.
display_Struct(&emp); is calling it’s definition by passing address of the structure “emp” to print the information of emp on console.
empData is an pointer variable of similar structure type which holds the address of emp structure and print all its information through -> operator.
In case of array I always recommend “Call by reference” method.You can follow below example to understand the concept.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#include <stdio.h> #include<string.h> typedef struct { unsigned char name[20]; unsigned int empId; double salary; }empInfo; void display_Struct(empInfo * ); //Function Prototype declaration int main() { empInfo emp[3]= { {"Amit",3800209,80000}, {"Ajit",3800215,70000}, {"Ajay",3800218,60000} }; display_Struct(emp); return 0; } void display_Struct(empInfo *empData) { int i=0; for(i=0;i<3;i++) { printf("Name=%s\t",(empData+i)->name); printf("empId=%u\t",(empData+i)->empId); printf("Salary=%lf\n",(empData+i)->salary); } } |
1 2 3 4 |
Output: Name=Amit empId=3800209 Salary=80000.000000 Name=Ajit empId=3800215 Salary=70000.000000 Name=Ajay empId=3800218 Salary=60000.000000 |