DAY 4
0

# 簡介函式

``````function add(a, b)
c = a + b
return c
end
``````

`c = a + b`這個表達式當中包含了一些訊息，他會先將`a``b`兩個相加之後，並且指定給`c`。如此一來我們就完成了將兩個數字相加的函式囉~~~

# 參數傳遞

Julia傳遞參數的機制是pass-by-sharing，個人認為跟call by reference沒有什麼兩樣就是了。

# 函式幹嘛用

``````function shape(x)
if x == paper
return "布"
elseif x == scissor
return "剪刀"
elseif x == stone
return "石頭"
end
end
``````

# 你需要的是一個矩陣

``````    | 布  剪刀  石頭
-------------------
布|  0   -1    1

``````

``````A = [2, 3, 5]
A[2]
``````

``````A = [0, -1, 1;
1, 0, -1;
-1, 1, 0]

A[1, 2]
``````

``````win = A[x+1, y+1]

x = shape(x)
y = shape(y)
if win == 0
println("平手")
else
println("你出" * x)  # 其中*是把兩個字串串接在一起的意思
println("電腦出" * y)
if win == 1
println("你贏了")
else
println("電腦贏了")
end
end
``````

# 函式的各種花樣

## 多個回傳值

``````funciton foo(a, b, c)
return (a, b, c)
end
``````

## 任意數量參數

``````function foobar(a, b, x::Vararg{Int64})
println(a)
println(b)
println(x)
end

foobar(1, 2, 3, 4)
``````

``````1
2
(3, 4)
``````

## 可選參數

``````function foo(a, b, c=10)
...
end
``````

``````foo(1, 2)
``````

``````foo(1, 2, 3)
``````

## 關鍵字參數

``````function bar(a, b; i=5, j=6)
...
end
``````

``````bar(1, 2, i=10)
``````

Julia語言—從入門到專案31