Friday 30 April 2021

Inter function communication or Parameter passing techniques in C

When a function is called the calling function may have to pass some values to the called function. There are two ways in which arguments can be passed to the called function.

Call by Value (OR) pass by value

In the call by value method, the called function creates duplicate variables to store the values of the arguments passed in it. Therefore the called function uses a copy of the actual parameters to perform its tasks. 

If the called function is supposed to modify the value of the parameters passed to it; then changes will not reflect in actual parameters. Because changes are done only copy of the actual parameters but not an original values.

Example:

void swap(int a, int b);
void main()
{
int a,b;
printf(“%d%d”,&a,&b);
printf(“a,b values before swapping %d %d”, a,b);
swap(a,b);
printf(“a,b values after swapping %d %d”, a,b);
getch();
}
void swap(int a, int b)
{
  int temp;
temp=a;
a=b;
b=temp;
}

Call by References (OR) Pass by address

In call by reference method, the called function doesn't create duplicate variables to store the value of arguments passed in it. Instead it stores the address of the actual parameters. If any changes made in called function will automatically reflect in the calling function, because the changes are done in the memory not to the values.

void swap(int *a, int *b);
void main()
{
int a,b;
printf(“%d%d”,&a,&b);
printf(“a,b values before swapping %d %d”, a,b);
swap(&a,*b);
printf(“a,b values after swapping %d %d”, a,b);
getch();
}
void swap(int *a, int *b)
{
  int *temp;
*temp=*a;
*a=*b;
*b=temp;
}

 

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top