The Ruby language makes it easy to create functions.

Function Syntax

def functionname(variable)
   return <value>


Your function can compute values and store them in local variables that are specific to the function. Those values can then be returned with the return statement.

def say_hello(name)
   var = “Hello, ” + name
   return var

The return statement also can be shortened for very simple functions into a single line

def say_hello(name)
   return “Hello, ” + name

You can simplify the function further. The last expression that is evaluated is automatically returned by the method. For example:

def say_hello(name)
   “Hello, ” + name

This would return the same value as the prior functions.

To call a function

function param1, param2




puts say_hello(“Geek”)

Profile Photo for Lowell Heddings Lowell Heddings
Lowell is the founder and CEO of How-To Geek. He’s been running the show since creating the site back in 2006. Over the last decade, Lowell has personally written more than 1000 articles which have been viewed by over 250 million people. Prior to starting How-To Geek, Lowell spent 15 years working in IT doing consulting, cybersecurity, database management, and programming work.
Read Full Bio »