Purpose of functions?

I’ve been scripting on and off for a couple of months and need to learn some things. My question is, what’s the purpose and intention of functions. I need help understanding how they would be implemented into a game or how they would be used. (Also, I tried looking on the DevForum and the API and I couldn’t find anything about how they would be relevant to use in a game and when you would use them.)

For more specifically about usage vvv

https://education.roblox.com/en-us/resources/functions-part-1#:~:text=Functions%20are%20sets%20of%20instructions,to%20use%20more%20than%20once.&text=Functions%20can%20be%20thought%20of%20like%20a%20recipe.

Functions allow scripts to run potentially long lines of code repeatedly throughout the script. Here’s an example

local function call()
    print('I was called!')
end

game.Players.PlayerAdded:Connect(function(player)
    if player:GetRankInGroup(0000) >= 255 then
        call()
    else
        print('I was not called!')
    end
end)

This allows you to call functions multiple times. For more information:

A function is just a block of code that can be used repeatedly, This is useful when your using a block of code multiple times. For example:

function makePartWithColorBlue()
    local newPart = Instance.new("Part", workspace)
    newPart.BrickColor = BrickColor.new("Blue")
end

makePartWithColorBlue()
makePartWithColorBlue()

-- In workspace you should see 2 blue parts

Of course you will do more advance stuff with function, such as adding parameters

function AddTwo(number)
   print(number + 2)
end

AddTwo(1)
AddTwo(2)
-- output should say:

-- 3
-- 4

Hope this helps!

Functions are the main mechanism for abstraction of statements and expressions in Lua. Functions can both carry out a specific task (what is sometimes called procedure or subroutine in other languages) or compute and return values.

Source: Here.

Ohhhh, I see. You called the function twice so it would repeat it two times. Also, I’m assuming that a function doesn’t loop infinitely or does it loop only once?

Nope it wouldn’t loop infinitely. Functions will be ignored until its called. When its called it will only execute code INSIDE the function.

That makes sense now. It only runs/executes inside the function, and once the function is called it would then be executed.

Yup! Exactly. Glad you understand it now! (Also mind making my post as the solution so other people could see it :') )

Functions let you store code and allow you to add certain arguments that the function needs to work like a player argument.

local function LevitatePlayer(Player, Power) -- Player and power are arguments that the player needs to fill in when calling this function
      local BodyVelocity = Instance.new("BodyVelocity", Player.Character.PrimaryPart)
      BodyVelocity.Velocity = Vector3.new(0,Power,0)
      BodyVelocity.P = math.huge
end

LevitatePlayer(game.Players.DankDinoDragon, 500)

You can also store functions inside of a table!

local WeaponClass = {}

WeaponClass.new(Position, ModelName)
      local WeaponsFolder = game.ReplicatedStorage.WeaponAssets
      local WeaponTemplate = WeaponsFolder:FindFirstChild(Model) 
      If WeaponTemplate ~= nil then
         local Weapon = WeaponTemplate:Clone()
         Weapon.Position = Position
         Weapon.Parent = workspace
      end
end

while true do
     wait(10)
     WeaponClass.new( Vector3.new(0,0,0), "Shiny Broadsword") -- We put the Position and Model parameters
-- You can get an item in a dictionarty like workspace.Part but YourDictionary.ThingInDictionary
end

There are also functions in events that run such as Part.Touched:Connect(YourFunctionHere)

local Part = game.Workspace.Lava

Part.Touched:Connect(TouchingPart) -- The player argument which gives us the part touching the lava
     local Player = game.Players:GetPlayerFromCharacter(TouchingPart.Parent) -- Another built in function but for this function we need to give the thing to check for a player
     if Player ~= nil then
         local Character = Player.Character
         Character:BreakJoints() -- Another built in function to kill a character
     end
end) 

-- OR...
local function DetectAndKill(TouchingPart)
  local Player = game.Players:GetPlayerFromCharacter(TouchingPart.Parent)
     if Player ~= nil then
         local Character = Player.Character
         Character:BreakJoints() 
     end
end

Part.Touched:Connect(DetectAndKill(TouchingPart)) -- We can put a premade function inside here

All in all, functions are pieces of code that you can run at any part of your script after the function was made. They can have arguments which you need to provide like a Player argument or a speed argument if you put that in your function. You can also store functions in tables and you can connect them to events to run that function when the event happens like Part.Touched:Connect(FunctionHere).

Hope this helped!

Edited to fix a few code mistakes!