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 trialSteve Rogers
3,285 Pointsmethods and arguments
Hello,
My task is:
Define a method named three that always returns the number 3. Also define a method named five that always returns the number 5. Neither method should take any arguments. (We know, these methods aren't especially useful. We just want to see if you know how to make a method return a particular value!)
But when I enter:
def three(param)
puts 3
end
def five(param) puts 3 end
three five
I get an error:
Bummer: wrong number of arguments (given 0, expected 1)
Any ideas? I know this is pretty basic but I think I am not understanding some core principles here so I wanted to make sure I get this question explained.
Thanks in advance!
def three(param)
3
end
def five(param)
3
end
Steve Rogers
3,285 PointsNice! Ya that was one thing I did manage to pick up from the video.
3 Answers
KRIS NIKOLAISEN
54,971 PointsA parameter is a variable in the method definition. An argument is the actual value that gets passed in.
For the following
def three(param)
param
is the parameter. When the three method is called passing in for example 10
three(10)
10 is the argument
KRIS NIKOLAISEN
54,971 PointsSome hints
- Neither method should take any arguments. This means there is no param.
- You should be returning values. Use
return
instead ofputs
- five should return 5. Both your methods currently have 3.
Steve Rogers
3,285 PointsI see. So:
def three 3 end
def five 5 end
got me the right answer.
To make sure I understand, a Method is a defined set of expressions that returns a value.
The parameters within the method are variables that can be called (here's where I'm a little confused with new terminology). The parameters are also referred to as arguments?
Thanks
Steve Rogers
3,285 PointsAwesome! Thanks!
KRIS NIKOLAISEN
54,971 PointsKRIS NIKOLAISEN
54,971 PointsHey I learned something from your answer. Ruby doesn't have to use the return keyword. In Ruby a method will return the value that was returned from the last evaluated statement.