owned this note
owned this note
Published
Linked with GitHub
# 編寫函數
2020, jeeeerrrpop
---
## Why use function?
我們可以對於出現很多次的操作將他寫成函數,之後需要用時直接呼叫函數並傳入對應的參數,能使程式碼更簡潔,增加程式碼的可讀性。
---
```cpp
if(a > b && c > b) mn = b;
else if(a > c && b > c) mn = c;
else mn = a;
// if we have min funciton
mn = min(a, min(b, c));
```
---
## Last Week
我們可以使用別人已經寫好的function
```cpp
pow(a, b);
strcpy(c, d);
isdigit(x);
strlen(s);
rand();
```
---
我也想要自己寫 function!
我希望能寫一個函數傳入a, b 回傳 a + b
```cpp
int a = 7;
int c = add(a, 122);
//c = 129
```
---
回顧一下
一般的函數大概會長這樣:
```cpp
回傳的型態 函數名稱(參數 1, 參數 2, ...) {
各種處理
return 回傳值;
}
```
因此定義一個函數也需要這種架構。
---
## define a function
```cpp
int add(int a, int b) {
int sum = a + b;
return sum;
}
int main() {
int a = 7;
int c = add(a, 122);
}
```
---
```cpp
Can it work?
int main() {
int a = 7, b = 1;
int c = add(a, b);
}
int add(int a, int b) {
int sum = a + b;
return sum;
}
```
---
## declare a function
需包含函數名稱、回傳型態、參數型態
```cpp
int add(int, int);
```
---
```cpp
int add(int, int);
int main() {
int a = 7;
int c = add(a, 122);
}
int add(int a, int b) {
int sum = a + b;
return sum;
}
```
---
## Example
Neoj289 福祿猴的反敗
輸入兩個球的半徑,輸出兩個球的表面積和。
---
我想寫一個函數去計算球的表面積
```cpp
#include <iostream>
const int PI = 3;
int BallSurfaceArea(int r) {
return 4 * PI * r * r;
}
int main() {
int r1, r2;
std::cin >> r1 >> r2;
std::cout << BallSurfaceArea(r1) + BallSurfaceArea(r2);
}
```
---
## Practice(1)
[neoj-225](https://neoj.sprout.tw/problem/225/)
---
## 關於回傳型態
double, char, int.....
void
---
```cpp
double abs(double x) {
if(x < 0) return -x;
else return x;
}
```
---
```cpp
#include <iostream>
void print(){
std::cout << "<(_ _)>\n";
}
int main() {
print();
for(int i = 0; i < 13; i++) print();
return 0;
}
```
---
如果在某些時候你想跳出這個 function
```cpp
void divide(double a, double b){
if(b == 0) {
std::cout << "Can't divide zero!\n";
return;
}
std::cout << a << " / " << b << " = " << a / b << "\n";
}
```
---
```cpp
void test() {
std::cout << "in" << std::endl;
return;
std::cout << "Will this be printed?" << std::endl;
}
```
---
## Parameter
int, double, char.....
int[], int*, ...
---
```cpp
int SumElement(int a[10], int n) {
int sum = 0;
for(int i = 0; i < n; i++) {
sum += a[i];
}
return sum;
}
// we can also write int a[], or int* a;
```
---
```cpp
int SumMatrix(int n, int m, int mat[10][10]) {
int sum = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
sum += mat[i][j];
return sum;
}
//we can also write mat[][10]
```
---
## pass-by-value
```cpp
#include <iostream>
void add7(int x) {
x += 7;
}
int main() {
int a = 0;
add7(a);
std::cout << a << std::endl;
return 0;
}
```
---
## pass-by-reference
[C++ reference wiki](https://en.wikipedia.org/wiki/Reference_(C%2B%2B))
```cpp
#include <iostream>
void add7(int &x) {
x += 7;
}
int main() {
int a = 0;
add7(a);
std::cout << a << std::endl;
return 0;
}
```
---
```cpp
void swap(int a, int b) {
int t = a;
a = b;
b = t;
}
void swap(int &a, int &b) {
int t = a;
a = b;
b = t;
}
```
---
What will happen?
```cpp
#include <iostream>
void add7(int x[], int id) {
x[id] += 7;
}
int main() {
int a[10] = {0};
add7(a, 5);
std::cout << a[5] << std::endl;
return 0;
}
```
並非把整個陣列傳進去,是傳了他的指標進去。
---
常見錯誤
```cpp
#include <iostream>
int SumElement(int a[], int n) {
int sum = 0;
for(int i = 0; i < n; i++) {
sum += a[i];
}
}
int main() {
int a[5] = {1, 2, 3, 4, 5};
SumElement(a, 5);
std::cout << sum << std::endl;
}
```
---
## Default Arguments
```cpp
int add(int a, int b, int MOD = 1000) {
return (a + b) % MOD;
}
int main() {
add(7122, 7122)
}
```
---
- 任何一個變數有設定default value,其後面所有變數也都要設定。
- 你使用函數時括號內的變數ㄧ樣會從頭一個一個傳入。
---
```cpp
int test(int a, int b = 2, int c = 3) {
cout << a << ' ' << b << ' ' << c << endl;
}
int main() {
test(1, 7);
}
```
---
#### Practice(2)
[neoj-226](https://neoj.sprout.tw/problem/226/)
#### Hw
[neoj-618](https://neoj.sprout.tw/problem/618/)
[neoj-514](https://neoj.sprout.tw/problem/514/)