Ticker

6/recent/ticker-posts

C Language (Call by value in C)

CALL BY VALUE IN C

In the 'Call by value' method, we pass only copy of the actual parameters to the function.
Therefore any changes made to the copy of parameter does not effect to the value of actual parameter.

Note: By default C programming language use the 'Call by value' method to the function parameters.





CALL BY VALUE EXAMPLE IN C
#include<stdio.h>
#include<conio.h>

// Define the swap function
void swap (int a, int b)
{
int c;
c = a;
a = b;
b = c;
}

void main()
{
clrscr();
int a, b;
printf("Enter value for A:");
scanf("%d", &a);
printf("Enter value for B:");
scanf("%d", &b);

printf("\nValues before call the swap function");
printf("\nValue of A: %d", a);
printf("\nValue of B: %d", b);

// Calling the 'swap' function 
swap(a, b);   

printf("\n\nValues after call the swap function");
printf("\nValue of A: %d", a);
printf("\nValue of B: %d", b);

getch();
}

----------------------------
         OUTPUT 
----------------------------

Enter value for A: 4
Enter value for B: 7

Values before call the swap function
Value of A: 4
Value of B: 7

Values after call the swap function
Value of A: 4
Value of B: 7















Post a Comment

0 Comments