Ruby Function (method) Syntax

By Lowell Heddings on December 31st, 1969

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”)

Lowell Heddings, better known online as the How-To Geek, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on if you'd like.

  • Published 12/31/69
More Articles You Might Like

Enter Your Email Here to Get Access for Free:

Go check your email!