Programming with C

⌘K
  1. Home
  2. Docs
  3. Programming with C
  4. Functions and Pointers
  5. Parameter Passing

Parameter Passing

This is of two types,

  1. Pass by value
  2. Pass by reference

Call by value

If data is passed by value, the data is copied from the variable used in for example main() to a variable used by the function. So, if the data passed (that is stored in the function variable) is modified inside the function, the value is only changed in the variable used inside the function. Any update made inside method will not affect the original value of variable in calling function.

  • The method of passing arguments by value is known as call by value.
  • In this method, the values of the actual arguments are copied to the formal parameters of the function.
  • If the arguments are passed by values, the changes made in the values of formal parameters inside the called function are not reflected back to the calling function

Program: Swapping two numbers using call by value (without using temporary variable)

#include<stdio.h>
void swap(int a,int b);
int main()
{
	int a,b;
	printf("Enter two numbers");
	scanf("%d%d",&a,&b);
	printf("Before Swapping\n");
	printf("a=%d\n",a);
	printf("b=%d\n",b);
	swap(a,b);
	printf("After Swapping\n");
	printf("a=%d\n",a);
	printf("b=%d\n",b);
	return 0;
}
void swap(int a,int b)
{
	a = a+b;
	b = a-b;
	a = a-b;
	printf("In function swap \n");
	printf("After swapping\n");
	printf("a=%d\n",a);
	printf("b=%d\n",b);
}

Output

Enter two numbers4 5
Before Swapping
a=4
b=5
In function swap
After swapping
a=5
b=4
After Swapping
a=4
b=5

Call by reference

If data is passed by reference, a pointer to the data is copied instead of the actual variable as is done in a call by value. Because a pointer is copied, if the value at that pointers address is changed in the function, the value is also changed in main().While passing parameter using call by address scheme , we are passing the actual address of the variable to the called function.

  1. Any updates made inside the called function will modify the original copy since we are directly modifying the content of the exact memory location.
  2. The method of passing arguments by address or reference is known as call by address or call by reference
  3. In this method, the address of the actual arguments are passed to the formal parameters of the function.
  4. If the arguments are passed by reference, the changes made in the values pointed to by the formal parameters in the called function are reflected back to the calling function

Example Program: Swapping of two numbers and changing the value of a variable using pass by reference

Swapping two numbers using call by reference  (without using temporary variable)

#include<stdio.h>
void swap(int *a,int *b);
int main()
{
	int a,b;
	printf("Enter two numbers");
	scanf("%d%d",&a,&b);
	printf("Before Swapping\n");
	printf("a=%d\n",a);
	printf("b=%d\n",b);
	swap(&a,&b);
	printf("After Swapping\n");
	printf("a=%d\n",a);
	printf("b=%d\n",b);
	return 0;
}
void swap(int *a,int *b)
{
	*a = *a + *b;
	*b = *a - *b;
	*a = *a - *b;
	printf("In function swap using reference \n");
	printf("After swapping\n");
	printf("a=%d\n",*a);
	printf("b=%d\n",*b);
}
Output
Enter two numbers4 5
Before Swapping
a=4
b=5
In function swap using reference
After swapping
a=5
b=4
After Swapping
a=5
b=4

Difference between call by value and call by reference

Views: 0

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments