1: In C + +, there are two main ways in which function parameters are passed, that is, value passing and reference passing. Value passing refers to passing a copy of the value of the actual parameter to the calling function when the function is called, so that if the value of the parameter is modified in the calling function, its change will not affect the value of the actual parameter. Whereas reference passing is the opposite, if a function is passed by reference, the value of the parameter is modified in the calling function, and its change affects the actual parameter.
The sample code is as follows:
//5.18.cpp: Defines the entry point of the console application. //#include"stdafx.h"#include<iostream>using namespacestd;voidSwapint& A,int& B)//This is a reference delivery method{ inttmp; TMP=A; A=b; b=tmp;}voidMain () {intx, y; cout<<"Please enter x"<<Endl; CIN>>x; cout<<"Please enter Y"<<Endl; CIN>>y; cout<<"swapping x and y by reference"<<Endl; Swap (x, y); cout<<"x="<< x <<Endl; cout<<"y="<< y <<Endl;}//once passed by reference, the original parameters changed.
View Code
Operation Result:
Introduction to C + + Classic-Example 5.18-exchanging values by reference