Ruby Ruby Basics Ruby Syntax Method Return Values

Steve Rogers
Steve Rogers
3,071 Points

methods 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!

methods.rb
def three(param)
  3  
end

def five(param)
  3
end
KRIS NIKOLAISEN
KRIS NIKOLAISEN
54,287 Points

Hey 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.

Steve Rogers
Steve Rogers
3,071 Points

Nice! Ya that was one thing I did manage to pick up from the video.

3 Answers

KRIS NIKOLAISEN
KRIS NIKOLAISEN
54,287 Points

A 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
KRIS NIKOLAISEN
54,287 Points

Some hints

  • Neither method should take any arguments. This means there is no param.
  • You should be returning values. Use return instead of puts
  • five should return 5. Both your methods currently have 3.
Steve Rogers
Steve Rogers
3,071 Points

I 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