Using Pass by Reference in a Simple Function in C++
This program will allow you to input two numbers. A function will be called to set the smallest number out of the two to 0 and to display the output in the main program.
void zeroSmaller(int &n1,int &n2);
using namespace std;
int main ()
{
int num1,num2;
cout<<"Input number 1: ";
cin>>num1;
cout<<"Input number 2: ";
cin>>num2;
zeroSmaller(num1,num2);
cout<<"The new values for number 1 and 2 are "<<endl<<num1<<" and "<<num2<<endl;
return 0;
}
void zeroSmaller(int &n1,int &n2)
{
if(n1<n2)
{
n1=0;
}
else
{
n2=0;
}
}
Comments
Post a Comment