# 引數與參數

``````#include <iostream>
using namespace std;

int increment(int n) {
n = n + 1;
return n;
}

int main() {
int x = 10;
cout << increment(x) << endl;
cout << x << endl;

return 0;
}
``````

``````#include <iostream>
using namespace std;

int increment(int *n) {
*n = *n + 1;
return *n;
}

int main() {
int x = 10;
cout << increment(&x) << endl;
cout << x << endl;

return 0;
}
``````

``````#include <iostream>
using namespace std;

void foo(char *s) {
cout << s << endl;
}

int main() {
char name[] = "Justin";
foo(name);
return 0;
}
``````

``````#include <iostream>
using namespace std;

int increment(int &n) {
n = n + 1;
return n;
}

int main() {
int x = 10;
cout << increment(x) << endl;
cout << x << endl;

return 0;
}
``````

``````#include <iostream>
using namespace std;

int foo(int &n) {
return n + 1;
}

int main() {
int x = 10;
foo(x);
foo(10);   // error: cannot bind non-const lvalue reference of type 'int&' to an rvalue of type 'int'
return 0;
}
``````

``````#include <iostream>
using namespace std;

int foo(const int &n) {
return n + 1;
}

int main() {
int x = 10;
foo(x);
foo(10);  // OK
return 0;
}
``````

C++ 11 開始可以使用 rvalue 參考，參數也可以宣告 rvalue 參考，當兩個函式各定義了 rvalue 參考與 const 的 lvalue 參考作為參數，使用常量呼叫時，編譯器會選擇 rvalue 參考的版本：

``````#include <iostream>
using namespace std;

void foo(int &&n) {
cout << "rvalue ref" << endl;
}

void foo(const int &n) {
cout << "lvalue ref" << endl;
}

int main() {
foo(10);  // 顯示 rvalue ref
return 0;
}
``````