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

Gareth Powell
PLUS
Gareth Powell
Courses Plus Student 7,016 Points

Is a method in Ruby the same as a function in other programming languages?

Hi All. Is the method used in this video (Method Arguments) the same as a function as found in other programming languages? It seems like the purpose of it is the same, i.e., it is reusable, but was unsure on the whether there is more to it and/or it is simply a naming thing? Thanks.

2 Answers

Ryan Field
PLUS
Ryan Field
Courses Plus Student 21,242 Points

Methods and functions are fundamentally the same thing, with a few key differences. A function is independent of any other code and can (usually) be called anywhere. Methods are functions, but are located within an object. An object can contain an assortment of variables and functions, but when you call those functions on the object, it is called a method.

In JavaScript, for example, you can define a function such as foo() and call it anywhere. JavaScript also has a Math object that is used for calculations such as rounding, but when you want to use Math.round(), we say that you are calling the .round() method (which is a function defined within the Math object).