函数定义 #
一、函数基础 #
1.1 标准语法 #
julia
function name(parameters)
body
return value
end
1.2 基本示例 #
julia
function greet(name)
println("Hello, $name!")
end
greet("Julia")
1.3 返回值 #
函数返回最后一个表达式的值:
julia
function add(a, b)
a + b
end
result = add(1, 2)
显式使用return:
julia
function add(a, b)
return a + b
end
1.4 简洁语法 #
单行函数可以使用简洁语法:
julia
add(a, b) = a + b
square(x) = x^2
greet(name) = println("Hello, $name!")
二、函数参数 #
2.1 位置参数 #
julia
function power(base, exponent)
base ^ exponent
end
power(2, 3)
2.2 默认参数 #
julia
function greet(name, greeting="Hello")
println("$greeting, $name!")
end
greet("Julia")
greet("Julia", "Hi")
2.3 关键字参数 #
使用;分隔关键字参数:
julia
function create_point(x, y; color="red", size=1)
println("Point at ($x, $y) with color=$color, size=$size")
end
create_point(1, 2)
create_point(1, 2; color="blue")
create_point(1, 2; color="blue", size=5)
create_point(1, 2, color="blue", size=5)
2.4 可变参数 #
使用...接收可变数量的参数:
julia
function sum_all(args...)
total = 0
for x in args
total += x
end
return total
end
sum_all(1, 2, 3, 4, 5)
2.5 可变关键字参数 #
julia
function process(; kwargs...)
for (key, value) in kwargs
println("$key = $value")
end
end
process(a=1, b=2, c=3)
三、类型注解 #
3.1 参数类型 #
julia
function add(a::Int, b::Int)::Int
a + b
end
add(1, 2)
add(1.0, 2.0)
3.2 返回类型 #
julia
function divide(a::Float64, b::Float64)::Float64
a / b
end
function process(x)::String
string(x)
end
3.3 多种类型 #
julia
function process(x::Union{Int, Float64})
x * 2
end
process(10)
process(10.5)
四、函数调用 #
4.1 位置参数调用 #
julia
function foo(a, b, c)
println("a=$a, b=$b, c=$c")
end
foo(1, 2, 3)
4.2 关键字参数调用 #
julia
function bar(; a, b, c)
println("a=$a, b=$b, c=$c")
end
bar(a=1, b=2, c=3)
bar(c=3, a=1, b=2)
4.3 混合调用 #
julia
function baz(a, b; c=3, d=4)
println("a=$a, b=$b, c=$c, d=$d")
end
baz(1, 2)
baz(1, 2; c=10)
baz(1, 2, c=10, d=20)
4.4 展开参数 #
julia
function sum_three(a, b, c)
a + b + c
end
args = [1, 2, 3]
sum_three(args...)
kwargs = (a=1, b=2, c=3)
bar(; kwargs...)
五、函数作为值 #
5.1 函数赋值 #
julia
f = x -> x^2
f(5)
g = add
g(1, 2)
5.2 函数作为参数 #
julia
function apply(f, x)
f(x)
end
apply(x -> x^2, 5)
apply(sqrt, 16)
5.3 函数返回函数 #
julia
function multiplier(n)
x -> x * n
end
times_3 = multiplier(3)
times_3(5)
六、文档字符串 #
6.1 添加文档 #
julia
"""
add(a, b)
Add two numbers together.
# Arguments
- `a`: First number
- `b`: Second number
# Returns
The sum of `a` and `b`
# Examples
```julia
julia> add(1, 2)
3
“”" function add(a, b) a + b end
text
### 6.2 查看文档
```julia
?add
七、实践练习 #
7.1 练习1:计算器 #
julia
function calculator(a, op, b)
if op == "+"
a + b
elseif op == "-"
a - b
elseif op == "*"
a * b
elseif op == "/"
b == 0 ? error("Division by zero") : a / b
else
error("Unknown operator: $op")
end
end
calculator(10, "+", 5)
calculator(10, "/", 2)
7.2 练习2:斐波那契 #
julia
function fibonacci(n::Int)
n <= 0 && return 0
n == 1 && return 1
fibonacci(n - 1) + fibonacci(n - 2)
end
fibonacci(10)
7.3 练习3:统计函数 #
julia
function statistics(arr::Vector{<:Number})
(
count = length(arr),
sum = sum(arr),
mean = sum(arr) / length(arr),
min = minimum(arr),
max = maximum(arr)
)
end
statistics([1, 2, 3, 4, 5])
7.4 练习4:高阶函数 #
julia
function compose(f, g)
x -> f(g(x))
end
double = x -> 2x
increment = x -> x + 1
double_then_increment = compose(increment, double)
double_then_increment(5)
八、总结 #
本章我们学习了:
- 函数定义:标准语法和简洁语法
- 函数参数:位置参数、默认参数、关键字参数
- 类型注解:参数类型和返回类型
- 函数调用:各种调用方式
- 函数作为值:赋值、传参、返回
- 文档字符串:函数文档
接下来让我们学习Julia的函数参数!
最后更新:2026-03-27