Creating Functions in Ruby
Zavexeon (633)

What is a function? A function is a block of code you can use over and over again, useful if you need to use a certain segment of code multiple times, but don't want to keep typing it all out.

Basic Syntax

For the most simple function, you would format it like this.

def function_name
    #your code here

The def keyword is telling Ruby that you are defining a function. The function_name is, well, the name of the function. At the end of the function we have the end keyword. This tells Ruby not to include any code below this keyword, essentially "ending" the function. Within the function you put your code. This can be any valid Ruby code.

Creating a simple function

Here is an example of a simple function.

def hello
    puts "Hello!"

Now we have a working function that should print Hello! to the console. But how do we get that code to activate?

That's called calling the function. We call a function by putting it's name in the code, BELOW THE FUNCTION. If you try to call a function above where it is defined, it won't work. This is how we call a function.

def hello
    puts "Hello!"

hello #we called it right here

This will output Hello! to the console.

Adding parameters

Functions can also take parameters, or data. When calling a function, you can also give it information for it to use. The syntax slightly changes, see.

def my_function(var)
     #your code

That parentheses we see with the "var" in it is our parameter. That will be the name of the variable that our function uses. You now have to call the function like this my_function(your_data). You input your data into a pair of parentheses following the calling of the function name. This can be a string, boolean, integer, etc.

Here is an example of how you could use a parameter.

def is_cool(thing)
    puts "#{thing} is cool!"


This would put Coding is cool! to the console.

You can also make functions take multiple variables. Just separate them by a comma like this.

def function_name(var1, var2)
    #code here

So now you call it like this, function_name(input1, input2). This will pass the value of both the inputs to the function.
#Using those variables
Well, now we can add parameters, but how do we use them? Easy! Here's a simple example.

def who_am_i(name, age)
    puts "I am #{name} and I am #{age} years old." #interpolation used here

who_am_i("Zavexeon", 16)

This puts to the console I am Zavexeon and I am 16 years old.

We can also do math.

def add(num1, num2)
    puts num1 + num2

add(1, 2)

This will output 3 to the console.

Welp, this tutorial is getting a little long, so look out for a second part! I hope you found this tutorial useful, and if you want more advanced info, wait for my second one. Have a good day everyone. 0w0

You are viewing a single comment. View All
JadielNunez (0)

thank you so much I would really love to work with you even though I am a beginner

Zavexeon (633)

@JadielNunez Oh hi. :) Depending on what you want to work on with me, I'd be happy to.