=begin
语法:def 方法名(参数1,参数2,。。。)
想要执行的动作
end
=end
def hello(name)
print("Hello, ", name, ".\n")
end
hello("Ruby") # =>Hello, Ruby.
#可以为参数指定预设值
def hello(name="Ruby")
print("Hello, ", name, ".\n")
end
hello() # =>Hello, Ruby.
hello("Newbie") # =>Hello, Newbie.
#当方法中不止一个参数时,预设值必须从右端的参数开始指定
def func(a, b=1, c=2) # 有两个参数可以省略,则应设计成右边2个可以省略
.....
end
def area(x, y, z)
xy = x*y
yz = y*z
xz = x*z
(xy + yz + xz)*2
end
p area(2, 3, 4) # => 52