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

Uses of arguments?

Asked by 9 years ago

We all know that arguments will be place inside the ( ) after the function. But the use of the argument really confused me. My dad explained that it is like math functions, such as: f(x) = x+1. X is 5. Function?

Is his statement true?

0
Your dad's insight is correct, yes. Basically, arguments to a function are like input. You use this input in the code however you want. Ex; function multiply(a,b) print(a*b) end print(multiply(5,3)) --> '15' Goulstem 8144 — 9y

2 answers

Log in to vote
0
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

I would just like to clarify an argument versus a parameter. You can't have one without the other, but they are not synonymous terms.

Basically, a parameter is in the parentheses when you create the function, while an argument is in the parentheses when you call it.

function add(a, b) --Parameters
    print(a + b)
end
add(1, 5) --Arguments

Take a look at CodeTheorem's more in-depth video about this.

Ad
Log in to vote
2
Answered by
drew1017 330 Moderation Voter
9 years ago

Well, an argument is basically additional information that the function, method etc (not only functions have arguments) might want to know to carry out whatever it's doing.

Take, for example, the TakeDamage method you can call on Humanoids.

Humanoid:TakeDamage

Naturally Roblox will not like that very much because you aren't specifying anything. Here's where arguments come in, as they are (in most cases) the universal way of adding extra information to otherwise abstract functions like this one. So we add an argument:

Humanoid:TakeDamage(25)

Which will cause the Humanoid to lose 25 health.

For functions, the arguments of a function are determined by the event that is calling it; Take for example the OnTouch event. When connected to a function, OnTouch has a argument that takes any word (most commonly used by scripters is 'hit', although it can be anything you want), as a reference to the object that touched the brick, for example:

function onTouch(hit)
    if hit.Parent:FindFirstChild('Humanoid') then -- Checks if hit's (presumably a body part of a player) parent (which, if it is a body part that was touched, would be the Character) has a Humanoid
        hit.Character.Humanoid:TakeDamage(25) -- If so, deals damage
    end
end
Part.Touched:connect(onTouch) -- Connector

, which will cause you to lose 25 health if you touch the brick.

Answer this question