current position:Home>Why should this code be int & n instead of int n? In C + +, under what circumstances do you need to be int & n

Why should this code be int & n instead of int n? In C + +, under what circumstances do you need to be int & n

2022-02-02 23:30:58 CSDN Q & A

#include

using namespace std;
int f1(int m,int&n){
for(n=10;n<100;n++){

if(n*n==m)break; 

}

if(n==100)return 0;
else return 1;
}

int f2(int x){
int p=x/100;
int q=x/10-p*10;
int r=x%10;
if(p!=q&&q!=r&&r!=p&&p<q&&q<r)return 1;
else return 0;
}

int main(){
int x=100,y;
for(x=100;x<1000;x++){
if(f1(x,y)==1&&f2(x)==1)
cout<<x<<"="<<y<<"^2"<<endl;
}
system("pause");
return 0;
}




Refer to the answer 1:

int &n In the function, it refers to the reference , here m and n use & The difference between and without , If you look at the output, you can find




Refer to the answer 2:



Refer to the answer 3:

f1 Function n This is the front. ‘&‘ Symbol , It means quoting , stay f1() Right in the function n The operation of , Is the corresponding in the main function y The operation of .




Refer to the answer 4:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202022330566625.html

Random recommended