← Back to Ruby Course | Chapter 7: Methods | Lesson 2 of 7

Parameters

In this page:

  1. Parameters

Parameters

Method parameters are listed in the definition's parentheses and become local variables available inside the method body. Arguments are matched positionally by default, in the order they're passed at the call site. Ruby raises an ArgumentError if the number of arguments doesn't match what the method expects (unless defaults or splat parameters are used).

Note: Ruby raises ArgumentError immediately if you call a method with the wrong number of required arguments.

Example: Parameters

markup
def greet(name, times)
  times.times { puts "Hello, #{name}!" }
end

greet("Sam", 2)
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.