DS | Practical-1 | Call by Value and Call by reference Program

 

GTU Data Structure Practical-1

Introduction to pointers. Call by Value and Call by reference.

CALL BY VALUE

  1. #include<stdio.h>
  2. void swap(int,int);
  3. int main()
  4. {
  5. int a,b;
  6. printf("\nEnter a Value of A=");
  7. scanf("%d",&a);
  8. printf("\nEnter a Value of B=");
  9. scanf("%d",&b);
  10. swap(a,b);
  11. printf("\nOld Values:");
  12. printf("A=%d B=%d \n",a,b);
  13. }
  14. void swap(int p,int q)
  15. {
  16. int tmp;
  17. tmp=p;
  18. p=q;
  19. q=tmp;
  20. printf("New Values After Swap:");
  21. printf("A=%d B=%d",p,q);
  22. }

CALL BY REFERENCE

  1. #include<stdio.h>
  2. void swap(int*,int*);
  3. int main()
  4. {
  5. int a,b;
  6. printf("\nEnter a Value of A=");
  7. scanf("%d",&a);
  8. printf("\nEnter a Value of B=");
  9. scanf("%d",&b);
  10. swap(&a,&b);
  11. printf("\nOld Values:");
  12. printf("A=%d B=%d \n",a,b);
  13. }
  14. void swap(int *p , int *q)
  15. {
  16. int tmp;
  17. tmp=*p;
  18. *p=*q;
  19. *q=tmp;
  20. printf("New Values After Swap:");
  21. printf("A=%d B=%d",*p,*q);
  22. }

Go to 

Home page: 

index: 

Practical 2 :

    Comments