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 trial

Ruby Ruby Basics (Retired) Ruby Methods Method Arguments: Part 2

Chris Beyer
Chris Beyer
969 Points

I am doing Ruby Basic and it is asking to create a method with two arguments named "subtract". I meant method "subtract"

I am in Ruby Basics Challenge 1 of two on methods. It says create a method called subtract that accepts two arguments.
I entered:

def subtract(5,1)

end

I don't know why it gives error expecting ')' Full error msg. SyntaxError: c80f3ab4-b76d-40b2-8d43-9e3282b6647a.rb:6: syntax error, unexpected tINTEGER, expecting ')' def subtract (5,1) ^

method.rb
def subtract (5,1)



end

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! The error message is correct. It's running into two unexpected integers. When defining a method the parameters will be the variable names that are associated with the values that are sent in when calling that method. Another way to say this is that we pass the arguments. The integers 5 and 1 are not valid ruby variable names. Take a look:

def subtract (x, y)

end

This defines a method named subtract that has two parameters. It will accept two arguments (which likely will be integers but could be anything) when we call/execute the method. While the method is running, the things we passed in will be referred to as x and y respectively. You can think of these as variable declarations that are only valid during the execution of this method.

Hope this helps! :sparkles:

andren
andren
28,558 Points

A method argument is a variable that gets set to a value when the function gets called, as such actual values like numbers or strings are not valid arguments when defining a method, an argument has to be a variable name, like this:

def subtract(a,b)
end

In the example above I define two arguments (a and b) those variables will be filled in with an actual value when the function is called. So for example if the function is called like this: "substract(5, 2)" then a will be set equal to 5 and b to 2. This is how method arguments work. It's how you pass data to a method when calling it.

Chris Beyer
Chris Beyer
969 Points

Thanks a million. Thanks for quick reply!