Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.