C++ maximizes three Numbers by reference

  • 2020-04-02 01:56:21
  • OfStack

This example illustrates the use of the reference as a function parameter.

See the code:


#include<iostream>
using namespace std;
int main(){
 void max(int &, int &);//Declare the method of a function when a reference is used as a function parameter
 int a[3];
 cout<<"please input three numbers:";
 cin>>a[0]>>a[1]>>a[2];
 max(a[0],a[1]);//Place the larger value in the first parameter
 max(a[0],a[2]);
 cout<<"max:"<<a[0]<<endl;
 return 0;
}
void max(int &i,int &j){
 if(i<j)i=j;//Notice that this function does not return a value
}

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/201310210951538.png ">

Related articles: