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

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!