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 trialFRANCIS F V ROBERTO
3,348 Points# I'm having an error here.why is it so? def add(a, b) puts a + b end add(1, 2)
it tells me to create a method that calls two arguments and returns the sum of the two arguments/numbers. I think there's nothing wrong with my code.
def add(a, b)
puts a + b
end
add(1, 2)
2 Answers
Sue Dough
35,800 PointsYou are not returning anything in your function. You should return like this instead of using puts.
def add(a, b)
return a + b
end
add(2, 3)
Tim Knight
28,888 PointsRuby explicitly returns the last value provided in a method. You don't need to specifically tell it to return. It's actually the Ruby idiom to not write it that way, but instead do:
def add(a, b)
a + b
end
add(2, 3)
FRANCIS F V ROBERTO
3,348 Pointscheers guys
Sidharta Surya Kusnanto
1,174 PointsSidharta Surya Kusnanto
1,174 PointsYou don't need to put in "puts" there. Ruby will automatically return the last line of the method, so if you use #puts there, #add will return nil, because #puts returns nil. Just having "a + b" inside your method will fix it, because then it will return the result of a + b. :)