Easy Written By tobyjing2005

Basics Of Functions Created on: 01-01-2019

This tutorial is about the basics of functions

The basics

A function is basically a block of code that can be called and used at anytime. You will come across many of

them as they're very useful.

A simple function may look like this:
function sayHello()

print("Hello!")
end

sayHello()

What you just saw was a simple function. All it does is print "Hello!" into the output window when called. When

you call a function you need to put an open bracket and a close bracket so that the script knows you're trying
to call the specified function.

You might be wondering why those brackets are there for... Well it's because of arguments.

Arguments

Arguments are variables you can pass to a function. They're also widely used for many differents reasons.

You can also have as many arguments as you want in a function.

Here's how an argument might be used:
function sayMessage(message)
    print(message)
end

local kindWords = "I love functions!"

sayMessage(kindWords)

This particular function prints whatever you pass to it, the argument.

When you call the function, you saw that you passed a variable to it, the variable being "kindWords"
and the function printed the variable into the output window.

This can be thought as a person calling the police on a criminal and passing the name of the suspect
to them which they can use.

Of course, you don't actually need to create a variable for the function to work, this code works the same:
function sayMessage(message)
    print(message)
end

sayMessage("I love functions!")

Return

Returns are useful parts of functions as they serve the role of returning a value back to the caller.

Example:
function calculateVolumeOfBrick(brick)
    local size = brick.Size
    local volume = size.X * size.Y * size.Z
    
    return volume
end

local volumeOfBrick = calculateVolumeOfBrick(workspace.Brick)

print(volumeOfBrick)

What this code does is it calculates the volume of a brick (in cubic studs) and returns it to the caller.

Why should I use functions?

Simple, to keep your code clean, concise, and easy to create. Compare:

Although example two looks short still, imagine changing the size, name, transparency, position, rotation and
parent. The function would be superior in keeping the code readable and concise while serving the same functionality.
function editBrick(brick, color, transparency, name)
    brick.Name = name
    brick.Color = color
    brick.Transparency = transparency
end

editBrick(workspace.Brick1, Color3.new(0, 0.5, 1), 0.5, "Ugly brick")
editBrick(workspace.Brick2, Color3.new(1, 0.4, 0.5), 0.8, "Weird brick")

VS

workspace.Brick1.Color = Color3.new(0, 0.5, 1)
workspace.Brick1.Transparency = 0.5
workspace.Brick1.Name = "Ugly brick"

workspace.Brick2.Color = Color3.new(1, 0.4, 0.5)
workspace.Brick2.Transparency = 0.8
workspace.Brick2.Name = "Weird brick"

Hope you have learned something new!

Easy Written By tobyjing2005

See tobyjing2005's profile on Roblox

Discussion