Results 1 to 3 of 3

Thread: Function Swap

  1. #1
    Junior Member
    Join Date
    Apr 2008
    Answers
    3

    Function Swap

    How do you write a function swap for C++?


  2. #2

    Re: Function Swap

    Hello, Either if it is in C or C++ you can use the best logic as given below:

    if a and b are the values (integer/float/....), then inside function if you use,
    a=a+b;
    b=a-b;
    a=a-b;
    The values of a and b gets swaped.

    The protype for a simple function fname is given below:
    void fname(int*,int*);

    While calling the function you have to pass the address of the values to be swaped as arguments.
    The definition is given below:
    fname(int *p,int *q)
    {
    *p=*p+*q;
    *q=*p-*q;
    *p=*p-*q;
    }


  3. #3
    Junior Member
    Join Date
    May 2008
    Answers
    2

    Re: Function Swap

    void Swap(int &a, int&b)
    {
    a=a-(b=(a=a+b)-b);
    }


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact