函数参数 #
一、位置参数 #
1.1 基本位置参数 #
julia
function add(a, b)
a + b
end
add(1, 2)
1.2 参数顺序 #
位置参数必须按顺序传递:
julia
function power(base, exponent)
base ^ exponent
end
power(2, 3)
power(3, 2)
1.3 类型注解 #
julia
function add(a::Int, b::Int)
a + b
end
add(1, 2)
add(1.0, 2.0)
二、默认参数 #
2.1 基本默认值 #
julia
function greet(name, greeting="Hello")
println("$greeting, $name!")
end
greet("Julia")
greet("Julia", "Hi")
2.2 多个默认值 #
julia
function create_config(host="localhost", port=8080, debug=false)
(host=host, port=port, debug=debug)
end
create_config()
create_config("example.com")
create_config("example.com", 443)
create_config("example.com", 443, true)
2.3 默认值表达式 #
julia
function process(data, timestamp=time())
(data=data, timestamp=timestamp)
end
process("hello")
2.4 默认值依赖 #
julia
function process(a, b=a*2)
(a=a, b=b)
end
process(5)
process(5, 20)
三、关键字参数 #
3.1 基本语法 #
使用;分隔位置参数和关键字参数:
julia
function create_point(x, y; color="red", size=1)
println("Point($x, $y): color=$color, size=$size")
end
create_point(1, 2)
create_point(1, 2; color="blue")
create_point(1, 2; color="blue", size=5)
3.2 调用语法 #
julia
create_point(1, 2, color="blue")
create_point(1, 2; color="blue")
3.3 必需关键字参数 #
julia
function process(; required)
println("Required: $required")
end
process(required=10)
process()
3.4 类型注解 #
julia
function process(; x::Int=0, y::Float64=0.0)
(x=x, y=y)
end
process()
process(x=10)
process(x=10, y=3.14)
四、可变参数 #
4.1 位置可变参数 #
使用...接收任意数量的参数:
julia
function sum_all(args...)
sum(args)
end
sum_all(1, 2, 3)
sum_all(1, 2, 3, 4, 5)
sum_all()
4.2 混合参数 #
julia
function process(first, rest...)
println("First: $first")
println("Rest: $rest")
end
process(1)
process(1, 2, 3, 4)
4.3 类型限制 #
julia
function sum_integers(args::Int...)
sum(args)
end
sum_integers(1, 2, 3)
sum_integers(1, 2.0, 3)
4.4 展开参数 #
julia
function add_three(a, b, c)
a + b + c
end
arr = [1, 2, 3]
add_three(arr...)
五、可变关键字参数 #
5.1 基本语法 #
julia
function process(; kwargs...)
for (key, value) in kwargs
println("$key = $value")
end
end
process(a=1, b=2, c=3)
5.2 获取关键字参数 #
julia
function process(; kwargs...)
kwargs_dict = Dict(kwargs)
haskey(kwargs_dict, :verbose) && println("Verbose mode")
kwargs_dict
end
process(verbose=true, debug=false)
5.3 混合关键字参数 #
julia
function process(required; default=10, kwargs...)
println("Required: $required")
println("Default: $default")
println("Others: $kwargs")
end
process(1)
process(1, default=20)
process(1, default=20, extra=30)
六、参数传递方式 #
6.1 值传递 #
Julia使用值传递,但对于可变类型,传递的是引用:
julia
function modify!(arr)
push!(arr, 4)
end
arr = [1, 2, 3]
modify!(arr)
arr
6.2 不可变类型 #
julia
function try_modify(x)
x = 10
return x
end
a = 5
try_modify(a)
a
6.3 惯例:!后缀 #
修改参数的函数通常以!结尾:
julia
function push_value!(arr, value)
push!(arr, value)
end
arr = [1, 2, 3]
push_value!(arr, 4)
七、实践练习 #
7.1 练习1:灵活的求和 #
julia
function flexible_sum(args...; ignore_negative=false)
total = 0
for x in args
if ignore_negative && x < 0
continue
end
total += x
end
return total
end
flexible_sum(1, 2, -3, 4)
flexible_sum(1, 2, -3, 4; ignore_negative=true)
7.2 练习2:配置构建器 #
julia
function build_config(; name, version="1.0", kwargs...)
config = Dict{Symbol, Any}(
:name => name,
:version => version
)
for (key, value) in kwargs
config[key] = value
end
return config
end
build_config(name="MyApp", debug=true, port=8080)
7.3 练习3:日志函数 #
julia
function log_message(level, message; timestamp=time(), kwargs...)
metadata = join(["$k=$v" for (k, v) in kwargs], ", ")
println("[$level] $timestamp: $message ($metadata)")
end
log_message("INFO", "Server started"; host="localhost", port=8080)
7.4 练习4:数学函数 #
julia
function polynomial(x, coeffs...; normalize=false)
result = 0.0
for (i, c) in enumerate(coeffs)
result += c * x^(i-1)
end
if normalize && length(coeffs) > 0
result /= sum(coeffs)
end
return result
end
polynomial(2, 1, 2, 3)
polynomial(2, 1, 2, 3; normalize=true)
八、总结 #
本章我们学习了:
- 位置参数:按顺序传递的参数
- 默认参数:带有默认值的参数
- 关键字参数:使用名称传递的参数
- 可变参数:接收任意数量的参数
- 参数传递:值传递和引用语义
接下来让我们学习Julia的返回值!
最后更新:2026-03-27