Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Function parameter default value

  • default
def welcome(name = "World")
  return "Hello #{name}!"
end

puts welcome
puts welcome("Foo")
Hello World!
Hello Foo!
  • Type definition for parameters
def welcome(name : String)
  return "Hello #{name}"
end

puts welcome "Foo"

# puts welcome 42
# Error: no overload matches 'welcome' with type Int32

def add(x, y : Int32)
  puts "#{x} #{y}"
end

add(2, 3)

# add(2, 3.1)
# Error: no overload matches 'add' with types Int32, Float64

add(2.1, 3) # this works