Creating functions with Ruby is both similar and different from other languages. This is because of Ruby’s design to be simple to use/program with, but in it, some people may find certain things difficult to handle.
A Simple Function
Like with Python, you’ll define a function using the def
keyword. An example can be seen below:
def greeting
puts "Hello, World!"
end
greeting
However, unlike other languages we’ve looked at, notice how you don’t have to put an empty set or parenthesis for the parameters that are not there.
Additionally, you can optionally put a return statement at the end of the function.
def greeting
puts "Hello, World!"
return
end
greeting
Calling the Function
Notice that you can call a function, without parameters, without putting the parenthesis. This is semi-unique to Ruby. There are a few languages that allow this, but not many.
In some ways, it makes sense, as you are not passing anything to it, so why would you need it? On the other hand, this is definitely different, and it might feel “wrong”.
What if I include the parentheses?
Ruby will let you include the empty parentheses if you want to. It runs the same, and does not display an error or even a warning.
This is more familiar if you are starting with another language such as Python functions, C/C++ function, Java Methods, etc.
greeting()
Adding Parameters
Of course, many functions take parameters to make them more powerful. Ruby can take these as well.
Since Ruby doesn’t require that you give a variable a data-type, it doesn’t require you to assign it one as a parameter either, similar to Python or JavaScript. Instead, simply put the parameters between the parenthesis next to the function name.
Here we include the value of the parameter in the string.
def greeting(name)
puts "Hello, #{name}!"
return
end
If you are going to have more than one parameter, you separate them with commas like most languages.
Calling a Function with Parameters
When you call a function with a parameter, you will often put that value, or variable, inside the parenthesis of the called function, just like with other languages.
greeting("Walter") # prints tout Hello, Walter
myName = "Bob"
greeting(myName) # prints out Hello, Bob
However, you don’t technically have to. Not even if there is more than one parameter being passed. The following code will have the same results.
greeting "Walter" # prints tout Hello, Walter
myName = "Bob"
greeting myName # prints out Hello, Bob
Functions with Default Parameter Values
You can specify a default value for a function, much like you would in Python, or many other languages. Simply put and equal with a parameter value after the parameter name.
def greeting(name = "there")
puts "Hello, #{name}!"
return
end
Returning a Value From a Function
We’ve already seen how we can use return, to return from a function, and we can use it to return a value.
def greeting(name)
return "Hello, #{name}!"
end
However, you don’t technically have to use return. By default, Ruby will return the last expression that it evaluated in the function.
def greeting(name)
"Hello, #{name}!"
end
Creating Functions with Ruby was originally found on Access 2 Learn
One Comment
Comments are closed.