9.12.2011

call by reference swap program

Q. what is calling by reference? How it is different from call by value? Write a C function to swap two given numbers using call by reference mechanism.

Ans.

When an argument is passed by reference, the caller actually allows the called function to modify the original variable's value.

  • Sends the address of a variable to the called function.
  • Use the address operator(&) in the parameter of the called function.
  • Anytime we refer to the parameter, therefore we actually referring to the original variable.
  • If the data is manipulated and changed in the called function, the original data in the function are changed.
/*swap function to interchange values by call by reference*/
#include<stdio.h>
#include<conio.h>
void swaping(int *x, int *y);
int main()
{
 int n1,n2;
 printf("Enter first number (n1) : ");
 scanf("%d",&n1);
 printf("Enter second number (n2) : "); 
 scanf("%d",&n2);
 printf("\nBefore swapping values:");
 printf("\n\tn1=%d \n\tn2=%d",n1,n2);
 swaping(&n1,&n2);
 printf("\nAfter swapping values:");
 printf("\n\tn1=%d \n\tn2=%d",n1,n2);
 getch();
 return 0;
}
void swaping(int *x, int *y)
{
  int z;
  z=*x;
  *x=*y;
  *y=z;
}

/***************** OUTPUT **************/
call by reference swap C program
Swap using call by reference

11 comments:

  1. How do we draw a flowchart to the following code ?

    #include
    void swap(int *, int *);
    main()
    {
    int a,b;
    printf("Enter two numbers : ");
    scanf("%d%d",&a,&b);
    printf("Values before swap are\n");
    printf("a=%d\tb=%d\n",a,b);
    swap(&a,&b);
    printf("values before swap are\n");
    printf("a=%d\tb=%d\n",a,b);
    return 0;
    }
    void swap(int *x, int *y)
    {
    int tmp;
    tmp=*x;
    *x=*y;
    *y=tmp;
    }

    ReplyDelete
    Replies
    1. Flowchart for your above program (call by reference) at:

      http://cprogrammingcodes.blogspot.in/2012/10/call-by-reference-swapping-flowchart.html

      Delete
  2. output should be values after swamp are

    ReplyDelete
  3. #include
    #include
    int main()
    {
    char str[30];
    char *p,*t;
    printf("Enter any string : ");
    gets(str);
    for(p=str ; *p!=NULL ; p++);
    for(t=str, p-- ; p>=t; )
    {
    if(*p==*t)
    {
    p--;
    t++;
    }
    else
    break;
    }
    if(t>p)
    printf("\nString is palindrome");
    else
    printf("\nString is Not palindrome");
    getch();
    return 0;
    }



    akash;

    ReplyDelete
    Replies
    1. @Akash,
      (You can write your any program problem!)

      All palindrome program at:

      http://cprogrammingcodes.blogspot.in/2012/01/palindrome-using-pointer.html

      Delete
  4. #include
    #include
    void swap (int*,int*);
    void main ()
    {
    int a,b;
    clrscr();
    printf("enter the two numbers:");
    scanf("%d %d"&a,&b);
    printf("numbers before swapping are: %d %d",a,b);
    swap(&a,&b);
    getch();
    }
    void swap (int *x,int *y)
    {
    int z;
    z=*x;
    *x=*y;
    *y=z;
    printf("\n numbers after swapping are: %d %d",*x,*y);
    }


    /* shubh jaiswal */

    ReplyDelete
  5. int p=10,q=20;
    printf("\nThe numbers before swapping");
    printf("p:= %d q:= %d",p,q);
    p ^=q ^=p ^=q;
    printf("\nThe numbers after swapping");
    printf("p:= %d q:= %d",p,q);

    ReplyDelete
  6. Anyone know about brute force algorithm?

    ReplyDelete
  7. I need algorithm for above program

    ReplyDelete