# Functions

int a = 3, b = 5;
std::swap(a, b);
std::cout << a << ' ' << b << "\n";
char s1[10] = "hello";
char s2[10] = "World";
int len1 = strlen(s1);
int len2 = strlen(s2);

Function 的用途/好處

• 減少重複編寫同樣的程式碼
• 編寫程式時邏輯更流暢
• 增加程式的可讀性
• 使 debug 更容易

# Functions How To

type function_name(parameters) {
/* do something */
return variable;
}
• type: function 會回傳什麼型別的變數
• function_name: function 的名字
• parameters: function 的參數
• return: function結束並回傳變數

# Functions How To

int plus_one(int number) {
return number + 1;
}

• type: 回傳一個整數
• function_name: 命名為 plus_one
• parameters: 傳入一個整數 number
• return: function 結束並回傳 number + 1

# Return Type

Function 可以回傳數值/變數

bool is_positive(int number) {
return number > 0;
}
double get_circle_area(int radius) {
const double PI = 3.14;
}

# Return Type

int a = 3, b = 5;
std::swap(a, b);
std::cout << a << ' ' << b << "\n";

# Return Type

void

void print_hello() {
std::cout << "hello\n";
}

void 即沒有回傳變數，連 return 都可以省略

# Parameters

int sum_array(int arr[], int n) {
int sum = 0;
for (int i = 0; i < n; i++)
sum += arr[i];
return sum;
}
int arr[100] = {1, 2, 3};
int sum = sum_array(arr, 100);

# Parameters

int sum_2d_array(int arr[][100], int n, int m) {
int sum = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
sum += arr[i][j];
return sum;
}
int arr[100][100];
int sum = sum_2d_array(arr, 100, 100);

# Parameters

Pass by value

void swap(int a, int b) {
int c = a;
a = b;
b = c;
}
int a = 3, b = 5;
swap(a, b);
std::cout << a << ' ' << b << "\n";

# Parameters

Pass by value

void swap(int a, int b) {
...
}

int c = 3, d = 5;
swap(c, d);

c的值被複製給a

d的值被複製給b

# Parameters

Pass by reference

void swap(int& a, int& b) {
int c = a;
a = b;
b = c;
}
int a = 3, b = 5;
swap(a, b);
std::cout << a << ' ' << b << "\n";

# Parameters

Pass by reference

void swap(int& a, int& b) {
...
}

int c = 3, d = 5;
swap(c, d);

a被視為跟c是同一個變數

b跟d亦同

# Parameters

void plus_one(int arr[]) {
arr[0]++;
}

int arr[10] = {0};
plus_one(arr);
std::cout << arr[0] << "\n";

# Parameters

Default parameters

int increment(int num, int delta=1) {
return num + delta;
}

int n = 3;
int inc1 = increment(n);
int inc2 = increment(n, 2);

# Parameters

Default parameters

default 的參數一定要放在最後面

int increment(int num, int delta=1);
int increment(int delta=1, int num);

# Return

Function 結束(並回傳)

return的功能:

1. 結束 Function 的執行
2. 回傳數值給呼叫者

# Return

int divide(int a, int b) {
if (b == 0)
return -1;
return a / b;
}

void 也可以 return

void print() {
return;
std::cout << "will not print\n";
}

# Return

int get_one() {
return 1;
}
int one = get_one();
std::cout << one << "\n";

# Return

int quadratic(int a, int b, int c) {
// solve...
return x1, x2;
}

x^2=1
x=\pm 1

• struct
• std::pair
• std::tuple

# Note on Functions

Function 必須在使用前宣告

int main() {
int a = 3, b = 5;
swap(a, b);
}

void swap(int a, int b) {
...
}
void swap(int a, int b) {
...
}

int main() {
int a = 3, b = 5;
swap(a, b);
}

# Note on Functions

Or... Forward Declaration

void swap(int a, int b);

int main() {
int a = 3, b = 5;
swap(a, b);
}

void swap(int a, int b) {
...
}

# Note on Functions

int find_max_and_min_and_median(int arr[]);
int find_max(int arr[]);

int find_min(int arr[]);

int find_median(int arr[]);

By JT

• 543