function Passing Value
Example 1:
1#include <iostream>2#include <string>3 using namespacestd;4 intMain () {5 stringstr1="I Love china!", str2="I Love jinan!";6 voidExchange (string*P1,string*p2);7cout<<"str1:"<<str1<<Endl;8cout<<"str2:"<<str2<<Endl;9Exchange (&str1,&str2);Ten //take str1 and Str2 's address to P1 and P2 Onecout<<"str1:"<<str1<<Endl; Acout<<"str2:"<<str2<<Endl; - return 0; - } the voidExchange (string*P1,string*p2) { - string*P3; -P3=P1;//P1 gave the address to P3. -P1=P2;//P2 gave the address to P1. +P2=P3;//P3 gave the address to P2. - //The result is that P1/P2/P3 addresses each other and does not affect the value of STR1/STR2 +}
1#include <iostream>2#include <string>3 using namespacestd;4 intMain () {5 stringstr1="I Love china!", str2="I Love jinan!";6 voidExchange (string*P1,string*p2);7cout<<"str1:"<<str1<<Endl;8cout<<"str2:"<<str2<<Endl;9Exchange (&str1,&str2);Ten //take str1 and Str2 's address to P1 and P2 Onecout<<"str1:"<<str1<<Endl; Acout<<"str2:"<<str2<<Endl; - return 0; - } the voidExchange (string*P1,string*p2) { - stringP3; -P3=*P1;//assign the value of the str1 that P1 refers to P3 -*P1=*P2;//assign the value of the str2 that P2 refers to P1 +*P2=P3;//assign the value of P3 to the str2 of P2 -}
Example 2:
1#include <iostream>2#include <string>3 using namespacestd;4 5 //Output p=26 voidfunctionintp1)7 {8p1=5;9 }Ten One intMain () A { - intp=2; - function (p); thecout<<p<<Endl; - return 0; -}
1#include <iostream>2#include <string>3 using namespacestd;4 5 //Output p=56 voidfunctionint*p1)7 {8*p1=5;9 }Ten One intMain () A { - intp=2; -Function (&p); thecout<<p<<Endl; - return 0; -}
C++