Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

What is the purpose of paramiters in functions?

Asked by
Nik1080 24
7 years ago

Recently I have been learning more about functions and the question came to me: What is the point of using parameters in functions? I just don't see the benefit in using them. It just seems to me like they do nothing and have no purpose. Can anyone clarify for me?

0
Think of parameters as local variables to receive input values. As opposed to a function's return value(s), which is its output. It is also important to understand the difference between "parameter" and "argument". The parameter is a variable, the argument is its value -- the value that was passed to a call to the function. Link150 1355 — 7y

1 answer

Log in to vote
5
Answered by
tkcmdr 341 Moderation Voter
7 years ago

Hey Nik1080,

Like mathematical functions, Lua functions are meant to perform repetitive jobs consistently. Also alike mathematical functions, Lua functions allow you to specify how you want the job to be done based on existing data.

Consider the following:

x = 1 + 2

The algebraic statement above will always be equal to three, because 1 + 2 is always three. Knowing what 1 + 2 is equal to is great, but it's not very useful.

Suppose, however, we want to know what any given number squared is. We may state the following, where y is any number.

x = y^2

It can also be rewritten as:

f(x) = x^2

f(x) will always be x to the second power. We can now supply any number we want and get the answer likewise.

f(13) = 13^2 = 169

f(2) = 2^2 = 4

f(9001) = 9001^2 = 81,018,001

We can even have multiple independent parameters:

f(x, y) = x^y

So that:

f(2, 2) = 2^2 = 4

What have we learned? Mathematical functions are a lot more useful than your average statements. We can change the parameters based on what we need. A function like this is okay:

function ChangePartColor()
    game.Workspace.Part.BrickColor = BrickColor.new("Bright red");
end;

But it's pretty... static. If we want to change the color of a different part, or use a different color, we'd have to make whole new function! Very bad, indeed. However, there is hope:

function ChangePartColor(part, color)
    part.BrickColor = color;
end;

With this function, we can now specify what part we want to change the color of, and what color we want it, with ease (not to mention, we don't need several versions of the same function!)

Without parameters, functions would be pretty useless. You may as well not use functions at all, then. But we both now know their use. Consider the following:

for i, child in ipairs(game.Workspace:GetChildren()) do
    if (child:IsA("Part")) then
        ChangePartColor(child, BrickColor.new("Bright red"));
    end;
end;

With merely five lines of code (plus the function,) you can change the color of every part directly under game.Workspace to Bright red. Nice! But what if we want a color based on i? We can do this:

for i, child in ipairs(game.Workspace:GetChildren()) do
    if (child:IsA("Part")) then
        if (i <= 10) then
            ChangePartColor(child, BrickColor.new("Bright red"));
        else
            ChangePartColor(child, BrickColor.new("New Yeller"));
        end;
    end;
end;

Now, this function makes the first 10 parts Bright red, then makes the rest New Yeller. Pretty awesome!

This only [begins to] scratch the surface of the power of functions, but it sheds light on their basic purpose. Thanks for using Scripting Helpers!

Have a nice day, Nik1080!

Truly yours, tkcmdr

0
Wow. That makes soo much more sense now! I studies functions in math not to long ago, but never made that connection! I now have a greater understanding of functions, and for that, I am truly thankful. Nik1080 24 — 7y
0
You're welcome, Nik! Have a nice day. c; tkcmdr 341 — 7y
Ad

Answer this question