#include<iostream> usingnamespace std; voidchange1(int a) { cout<<"The initial value in the function is:"<<a<<endl; a=5; cout<<"The final value in the function is:"<<a<<endl; } intmain() { int a=4; cout<<"The initial value is:"<<a<<endl;; change1(a); cout<<"The final value is:"<<a<<endl; return0; }
#include<iostream> usingnamespace std; voidchange2(int *a)//传地址/按地址传递 { cout << "The initial value in the function is:" << *a << endl; cout << "The initial address in the function is:" << a << endl; *a = 3; cout << "The final value in the function is:" << *a << endl; cout << "The final address in the function is:" << a << endl; } intmain() { int a=4; cout<<"The initial value is:"<<a<<endl; cout<<"The initial address is:"<<&a<<endl; cout<<endl; change2(&a); cout<<endl; cout<<"The final value is:"<<a<<endl; cout<<"The final address is:"<<&a<<endl; return0; }
#include<iostream> usingnamespace std; voidchange3(int &a) { cout<<"The initial value in the function is:"<<a<<endl; a=5; cout<<"The final value in the function is:"<<a<<endl; } intmain() { int a=4; cout<<"The initial value is:"<<a<<endl; change3(a); cout<<"The final value is:"<<a<<endl; return0; }