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

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