Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby

I have no idea what the parenthesis do..

Im sitting here understanding how everything works thus far, and all of my code is working... BUT i just have no clue why we are putting variables next to the method?..i don't know how that is effecting the method whats so ever! Can someone please explain this to me, what do the parenthesis next to the method even mean, even without a variable...

http://gyazo.com/116ec2842d9abdb6906b387ad269b736

6 Answers

Reggie,

The parentheses are there to hold parameters, nothing more. If they're empty, that means no parameters are taken or required. Most (but not all) languages use a similar syntax.

"Return", by the way, means "spit out a value from this function so the rest of the program can use it". In all programming languages, "scope" constrains where values can be seen and used.

My best advice is: don't overthink it, especially the meaning of syntax words.

William Li
PLUS
William Li
Courses Plus Student 26,867 Points

Okay, when you defining a method, the variables inside the parenthesis is called "parameters", a method can take 0 or more parameters, you can name them however you like.

for example, if you wanna define an add method that return the sum of two integers, you could do so:

def add(a, b)
  a+b
end

this method would return the sum of a and b, a.

a and b are the sum method's parameters, but u can name them i or j, or whatever you like.

Now regarding the method without parameters, here is one example

def hello()
  puts 'hello world'
end

Since this method just output 'hello world' to the screen, it doesn't need any parameter to do it job, so the method takes 0 argument.

Now, very important, parenthesis in Ruby is often optional, the method definition is one example of that.

def add a, b
  a+b
end

you can define the add function like above, and it works the same. But people usually put parenthesis there for better readability.

Also for method that takes 0 argument, we generally omit the parenthesis.

def hello
  puts 'hello world'
end

Oh ok so i think i understand now, so.. the method is trying to find the sum of a + b but it can not output the value until the parameters variables tell the method what a + b equals. So thats why you must clarify to the method with parameters what a + b equals in order for it to do its job? If i'm understanding this right.. But one thing i forgot to ask, and i don't quite understand is what is return?..like when you return something, were are you returning it to? Ruby? or what?

William Li
PLUS
William Li
Courses Plus Student 26,867 Points

Well, regarding return keyword, it happens that return is also optional when you defining a method, by default, the value of last statement in the method body is the method's return value. Take a look at this example

def some_calculation(a, b, c)
  a + b
  b + c
  c / a
  a - b
end

In this method, Ruby will calculate every line from top to bottom, and the return value is a-b because it's the last statement in the method definition, I can of course explicitly put a return keyword there like this:

 def some_calculation(a, b, c)
  a + b
  b + c
  c / a
  return a - b
end

And the result is the same, but people generally don't do that.

This helped and all but what does return mean, XD! like what does it mean to "return"? Sorry >.< I dont even know what that keyword does or stands for :x

Is it different than the ruby track im learning at treehouse at the moment?