Sunday, January 23, 2011

CPP_Function Swape & Sum ( 1- 4 type)

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;

int a,b; // globle variable

int main()
{   void swap(void); //nohave parameter
    system("clear");
    cout<<"\nInput a, b : ";
    cin>>a>>b;
    cout<<"\nBefore swap a="<<a<<",b="<<b<<endl;
    swap();
    cout<<"\nAfter swap a="<<a<<", b="<<b<<endl;
    return 0;
}
void swap(void)
{   int X; // X is local variable
    X=a;
    a=b;
    b=X;
}
//-------------------------------------


==================================================

#include<stdio.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{   int a,b;    //local variable
    void swap(int&,int&); //parameter is integer
    system("clear");
    cout<<"\nInput a,b :";
    cin>>a>>b;
    cout<<"\Before swap a="<<a<<", b="<<b<<endl;
    swap(a,b); // Call by value
    cout<<"\After swap a="<<a<<", b="<<b<<endl;
    return 0;
}
void swap(int& a,int& b) //Call by value
{   int x; //local variable
    x=a;
    a=b;
    b=x;
}


==================================================

#include<stdio.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{   int a,b;    //local variable
    void swap(int*,int*); //parameter is integer
    system("clear");
    cout<<"\nInput a,b :";
    cin>>a>>b;
    cout<<"\Before swap a="<<a<<", b="<<b<<endl;
    swap(&a,&b); // Call by referrence
    cout<<"\After swap a="<<a<<", b="<<b<<endl;
    return 0;
}
void swap(int *a,int *b) //Call by referrence
{   int x; //local variable
    x=*a;
    *a=*b;
    *b=x;
}


==================================================

#include<stdio.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{   int a,b,Sums(int,int);    //local variable
    void swap(int*,int*); //parameter is integer
    system("clear");
    cout<<"\nInput a,b :";
    cin>>a>>b;
    cout<<"\Before swap a = "<<a<<", b = "<<b<<endl;
    swap(&a,&b); // Call by referrence
    cout<<"\After  swap a = "<<a<<", b = "<<b<<endl;
    cout<<"\nSum a+b = "<<Sums(a,b)<<endl;//Calll by value
    return 0;
}
//------------------------------------------------
void swap(int *a,int *b) //Call by referrence
{   int x; //local variable
    x=*a;
    *a=*b;
    *b=x;
}
//-------------------------------------------------
int Sums(int a,int b) //Call by value
{   int result; //local variable
    result = a+b;
    return result;
}


==================================================

No comments:

Post a Comment