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 trialSY LOC
3,233 PointsCreate 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.
def add(a, b)
puts a + b
end
puts add(2, 3)
1 Answer
Salman Akram
Courses Plus Student 40,065 PointsYou 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
3,233 PointsSY LOC
3,233 PointsOK, I got it. maybe a dumb question. What does 'return' do?
Salman Akram
Courses Plus Student 40,065 PointsSalman Akram
Courses Plus Student 40,065 Pointsreturn 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
3,233 PointsSY LOC
3,233 PointsThank you Salman Akram. All is clear.