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

Alphonse Cuccurullo
Alphonse Cuccurullo
2,513 Points

So im trying to grip def functions, Mind helping me with my syntax.

def times_five
puts" Five of these"
5.times do |item|
puts item
end
end 

puts times_five()

Im having trouble understanding def. Like if is it there so i dont keep typing the same code over and over? How often is it used? Also mind correcting my syntax? The goal there was to access the def whenever i wanted to type something down 5 times.

2 Answers

Hi Alphonse!

What you are doing in the code above is making, what is called, a method. A method is basically just a piece of code that you would like to re-use again and again so you have given a name to it. Just like how if you want to use a certain value again and again you store it in a variable.

When you go ahead and 'call' the times_five function you have set. It will run through the code inside of the function which is defined using the 'def' keyword.

Here is an example of a function below and beneath the function is it's call.

def this_is_a_function() #This is where the function is declared.
    puts 'This is a function and every time its called this will print.'
end

this_is_a_function() #This is the call to the function.

I hope that I managed to help you out here. If you do have any more questions, however, then don't hesitate to ask them!

-Luke

Alphonse Cuccurullo
Alphonse Cuccurullo
2,513 Points

Ohhhh ok that does make sense thank you. As far as another question ummm if you can help me with my syntax that i posted earlier. Because my method wont print. Also lets say i make a variable after the def function then can i iterate the variable with the method like this.........

variable = 5

puts variable[times_five()]

Yeah sure. So if we take a look at your method below:

def times_five()
    puts "Five of these"
    5.times do |item|
        puts item
    end
end

times_five()

Give that a go, you shouldn't need to put 'puts' before you are calling your function because, as you can see inside your function, you are already using 'puts' to print out the values.

Also, you can't really do what you are wanting with the variable iteration in that way. However, there is a really great thing that you will learn about shortly called parameters which will allow you to do this!

-Luke

Alphonse Cuccurullo
Alphonse Cuccurullo
2,513 Points

Thanks alot luke greatly appreciate your help. =)

No problem, glad I could help you out Alphonse!

Don't forget to mark a 'Best Answer' so the other members of the community know that your problem has been solved!