0. 导论

In Julia, a function is an object that maps a tuple of argument values to a return value. Julia functions are not pure mathematical functions, in the sense that functions can alter and be affected by the global state of the program. The basic syntax for defining functions in Julia is:

  1. julia> function f(x,y)
  2. x + y
  3. end
  4. f (generic function with 1 method)

在Julia中也存在一种更简便的函数定义方式,此方式被称为“分配模式”(assignment form)

  1. julia> f(x,y) = x + y
  2. f (generic function with 1 method)

正如上例所示: assignment form要求函数体必须是简单表达式,不能是复合表达式

函数f不带括号的话,可以将函数当成变量一样“传值”

  1. julia> g = f
  2. julia> g(2,3)
  3. 5

Unicode编码都可以用作函数名

  1. julia> ∑(x,y) = x + y
  2. (generic function with 1 method)
  3. julia> ∑(2, 3)
  4. 5

1. 参数传递(Argument Passing Behavior)

Julia function arguments follow a convention sometimes called “pass-by-sharing”, which means that values are not copied when they are passed to functions. 现在的“动态语言”基本都遵循这个规则

你需要了解以下: pass-by-value pass-by-reference pass-by-object pass-by-sharing

2. return 关键字

和Python类似,return以为函数“结束”

  1. function g(x,y)
  2. return x * y
  3. x + y
  4. end
  5. julia> f(x,y) = x + y
  6. f (generic function with 1 method)
  7. julia> function g(x,y)
  8. return x * y
  9. x + y
  10. end
  11. g (generic function with 1 method)
  12. julia> f(2,3)
  13. 5
  14. julia> g(2,3)
  15. 6

Of course, in a purely linear function body like g, the usage of return is pointless since the expression x + y is never evaluated and we could simply make x * y the last expression in the function and omit the return. In conjunction with other control flow, however, return is of real use.
纯线性函数,return没有太大意义,因为g中的x + y根本没有运行 😂
return更多的用在分支语句中

  1. julia> function hypot(x,y)
  2. x = abs(x)
  3. y = abs(y)
  4. if x > y
  5. r = y/x
  6. return x*sqrt(1+r*r)
  7. end
  8. if y == 0
  9. return zero(x)
  10. end
  11. r = x/y
  12. return y*sqrt(1+r*r)
  13. end
  14. hypot (generic function with 1 method)
  15. julia> hypot(3, 4)
  16. 5.0

A return type can also be specified in the function declaration using the :: operator. This converts the return value to the specified type.
在Julia中,::运算符可以设定返回值得类型

  1. julia> function g(x, y)::Int8
  2. return x * y
  3. end;
  4. julia> typeof(g(1, 2))
  5. Int8

3. 运算符作为函数

Julia中很有意思的是,很多运算符可以通过特殊的语法,直接当成函数使用

  1. julia> 1 + 2 + 3
  2. 6
  3. julia> +(1,2,3)
  4. 6

The infix form is exactly equivalent to the function application form – in fact the former is parsed to produce the function call internally. This also means that you can assign and pass around operators such as + and * just like you would with other function values
运算符可以之间传递给变量名,通过“前缀”变成函数

  1. julia> f = +;
  2. julia> f(1,2,3)
  3. 6

4. 匿名函数

类似于Python中的lambda函数

  1. julia> x -> x^2 + 2x - 1
  2. #1 (generic function with 1 method)
  3. julia> function (x)
  4. x^2 + 2x - 1
  5. end
  6. #3 (generic function with 1 method)

The primary use for anonymous functions is passing them to functions which take other functions as arguments.
匿名函数主要用来把变量传递给函数,再将函数作为参数传给其他函数(请默念3遍)
匿名函数是一次使用,一次消费的,不能重用

  1. julia> map(x -> x^2 + 2x - 1, [1,3,-1])
  2. 3-element Array{Int64,1}:
  3. 2
  4. 14
  5. -2

An anonymous function accepting multiple arguments can be written using the syntax (x,y,z)->2x+y-z. A zero-argument anonymous function is written as ()->3. The idea of a function with no arguments may seem strange, but is useful for “delaying” a computation. In this usage, a block of code is wrapped in a zero-argument function, which is later invoked by calling it as f.

  • 匿名函数支持多参数,0参数

  • 0参数用来“延迟”计算

5. 元组(Tuple)

Julia has a built-in data structure called a tuple that is closely related to function arguments and return values. A tuple is a fixed-length container that can hold any values, but cannot be modified (it is immutable). Tuples are constructed with commas and parentheses, and can be accessed via indexing:

Julia的元组和Python非常相似:

  • Tuple是Julia内置的

  • Tuple主要用来传递参数和获得返回值

  • Tuple中的元素可以说任何合法数据类型

  • Tuple是不可变类型

  • Tuple可以通过逗号和括号创建

  1. julia> (1, 1+1)
  2. (1, 2)
  3. julia> (1,)
  4. (1,)
  5. julia> x = (0.0, "hello", 6*7)
  6. (0.0, "hello", 42)
  7. julia> x[2]
  8. "hello"

Notice that a length-1 tuple must be written with a comma, (1,), since (1) would just be a parenthesized value. () represents the empty (length-0) tuple.

  • 只有一个元素的元组,该元素后面必须加逗号

  • ()表示空元组

6. 命名元组(Named Tuple)

(待续,2018年8月19日夜)