C++ Function Call by Reference

Master C++ with Real-time Projects and Kickstart Your Career Start Now!!

Program 1

// Function in CPP
#include<iostream>
using namespace std;
int swap(int *a,int *b);
int main()
{
     system("cls");
     int a,b;
     cout<<"Enter two number";
     cin>>a>>b;
     cout<<"\nBefore swaping: "<<a<<"  "<<b;
     swap(&a,&b); //local 
     cout<<"\nAfter swaping: "<<a<<"  "<<b;
     return 0;
}
int swap(int *a,int *b)  //local
{
    *a=*a+*b;
    *b=*a-*b;
    *a=*a-*b;
}



//void display(int *a); //declare
// int main()
// {
//      system("cls");
//      int a=50;     //local
//      display(&a);   //adress as an argument  /// call by reference 
//      cout<<a;  //a=50
//      return 0;
// }
// void display(int *a) //  pointer 
// {
//         ++*a;
// }

Program 2

// Function in CPP
#include<iostream>
using namespace std;
 int a=50;   //global
//void first();
void display();
int main()
{
    // int a =10;  //local
     system("cls");
    // cout<<a;  //10
    // first();
    // cout<<endl<<a;  //10
    display();
     return 0;

}
void display()
{
    cout<<"\nHello";
    cout<<"\nDataFlair";
    return;
    cout<<"\nIndore";
}
// void first()
// {
//     a=a+10;
//     cout<<"\n with in first: "<<a;
// }

Did you like this article? If Yes, please give DataFlair 5 Stars on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *