Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Basics (Retired) Ruby Methods Method Returns: Part 1

SY LOC
SY LOC
3,233 Points

Create a method called "add" that takes two arguments and returns the sum of two numbers.

I typed below and it does't work. I created 2 arguments and give it a sum.

method.rb
def add(a, b)
  puts a + b
end

puts add(2, 3)

1 Answer

You don't have to use twice puts statements, it should have either return or without return to sum up automatically. ;)

Example:

def add(a, b)
  return a + b
end

puts add(2, 3)
SY LOC
SY LOC
3,233 Points

OK, I got it. maybe a dumb question. What does 'return' do?

return is to return the values of the last statement being executed in the method in order to get the values you put numbers(2, 3) outside definition method (def add), however, puts statements is like only displaying the message you type inside definition method but haven't received numbers outside.

SY LOC
SY LOC
3,233 Points

Thank you Salman Akram. All is clear.