Julia 函數和流程控制

關注公共號:小程在線

關注CSDN博客:程志偉的博客

 

1.函數

julia> f(x,y) = x + y
f (generic function with 1 method)
julia> f(5,6)
11

julia> f(5,6)
11

julia> g = f
      g(8,9)
17

2.return關鍵字

julia> function h(x,y)
    return x * y
    x + y
end
h (generic function with 1 method)

julia> h(8,9)
72

julia> f(8,9)
17

julia> function hypot(x,y)
           x = abs(x)
           y = abs(y)
           if x > y
               r = y/x
               return x*sqrt(1+r*r)
           end
           if y == 0
               return zero(x)
           end
           r = x/y
           return y*sqrt(1+r*r)  
       end
hypot (generic function with 1 method)

julia> hypot(7,8)
10.63014581273465

julia> hypot(3,4)
5.0

julia> hypot(6,8)
10.0

3.元組

julia> x = (0.0, "hello", 6*7)
(0.0,"hello",42)

julia> x[2]
"hello"

4.多返回值

Julia 中,一個函數可以返回一個元組來實現返回多個值。不過,元組的創建和消除都不一定要用括號,這時候給人的感覺就是返回了多個值而非一個元組。比如下面這個例子,函數返回了兩個值:

julia> function foo(a,b)
           a+b, a*b
       end
foo (generic function with 1 method)

julia> foo(5,6)
(11,30)

julia> x,y = foo(5,6)
(11,30)

julia> x
11

julia> y

 

5.變參函數

定義有任意個參數的函數通常是很方便的。 這樣的函數通常被稱爲變參函數 (Varargs Functions), 是“參數數量可變的函數”的簡稱。 你可以通過在最後一個參數後面增加一個省略號來定義一個變參函數:

julia> bar(a,b,x...) = (a,b,x)
bar (generic function with 1 method)

julia> bar(1,2)
(1, 2, ())

julia> bar(1,2,3)
(1, 2, (3,))

julia> bar(1, 2, 3, 4)
(1, 2, (3, 4))

julia> bar(1,2,3,4,5,6)
(1, 2, (3, 4, 5, 6))

 

二、流程控制

2.1 複合表達式

有時一個表達式能夠有序地計算若干子表達式,並返回最後一個子表達式的值作爲它的值是很方便的。Julia 有兩個組件來完成這個: begin 代碼塊 和 (;) 鏈。這兩個複合表達式組件的值都是最後一個子表達式的值。

julia> z = begin
                  x = 1
                  y = 2
                  x + y
              end
3

julia> z = (x = 1; y = 2; x + y)
3

julia>

julia> begin x = 1; y = 2; x + y end
3

julia> (x = 1;
               y = 2;
               x + y)
3

 

2.2條件表達式

條件表達式(Conditional evaluation)可以根據布爾表達式的值,讓部分代碼被執行或者不被執行。

julia> function test(x, y)
                  if x < y
                      println("x is less than y")
                  elseif x > y
                      println("x is greater than y")
                  else
                      println("x is equal to y")
                  end
              end
test (generic function with 1 method)

julia> test(4,5)
x is less than y

julia> test(5,4)
x is greater than y

julia> test(3,3)
x is equal to y

 

julia> function test(x,y)
                  if x < y
                      relation = "less than"
                  elseif x == y
                      relation = "equal to"
                  else
                      relation = "greater than"
                  end
                  println("x is ", relation, " y.")
              end
test (generic function with 1 method)

julia> test(8,9)
x is less than y.

 

a ? b : c

在 ? 之前的表達式 a, 是一個條件表達式,如果條件 a 是 true,三元運算符計算在 : 之前的表達式 b;如果條件 a 是 false,則執行 : 後面的表達式 c。注意,? 和 : 旁邊的空格是強制的,像 a?b:c 這種表達式不是一個有效的三元表達式(但在? 和 : 之後的換行是允許的)。

julia> x = 8; y = 10;

julia> println(x < y ? "less than" : "not less than")
less than

julia> x = 8; y = 4;

julia> println(x < y ? "less than" : "not less than")
not less than

2.3 重複執行:循環

有兩個用於重複執行表達式的組件:while 循環和 for 循環。下面是一個 while 循環的例子:

julia> i=1;

julia> while i <= 5
                  println(i)
                  global i += 1
              end
1
2
3
4
5

while 循環會執行條件表達式(例子中爲 i <= 5),只要它爲 true,就一直執行while 循環的主體部分。當 while循環第一次執行時,如果條件表達式爲 false,那麼主體代碼就一次也不會被執行。

for 循環使得常見的重複執行代碼寫起來更容易。 像之前 while 循環中用到的向上和向下計數是可以用 for 循環更簡明地表達:

julia> for i = 1:5
                  println(i)
              end
1
2
3
4
5

爲了方便,我們可能會在測試條件不成立之前終止一個 while 循環,或者在訪問到迭代對象的結尾之前停止一個 for循環,這可以用關鍵字 break 來完成:

julia> i = 1;

julia> while true
                  println(i)
                  if i >= 5
                      break
                  end
                  global i += 1
              end
1
2
3
4
5

julia> for j = 1:1000
                  println(j)
                  if j >= 5
                      break
                  end
              end
1
2
3
4
5

在某些場景下,需要直接結束此次迭代,並立刻進入下次迭代,continue 關鍵字可以用來完成此功能:

julia> for i = 1:10
                  if i % 3 != 0
                      continue
                  end
                  println(i)
              end
3
6
9

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章