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

Whats the purpose of defining a function into a variable?

Asked by 4 years ago
Edited 4 years ago
function PrintFunction(SomeThingToPrint)


    print(SomeThingToPrint)


end



local Variable  = PrintFunction('Nooooob')




whats the point of doing this?

0
You don't need a variable to call a function, you can simply just create a function and call it by saying PrintFunction(SomeThingToPrint)) ISkyLordDoge 37 — 4y

1 answer

Log in to vote
2
Answered by 4 years ago

Whenever you call a function, it returns something. Unless you set what that something is, it is always nil. Now In your demonstration it doesn't make much of a difference, but let's say that you had a function that returned a value, and you wanted to use that value. That is where it becomes useful. For example:

function PrintFunction(SomeThingToPrint)

    print(SomeThingToPrint) -- we print out the value we sent to output

    return true -- We return true 

end



local Variable  = PrintFunction('Nooooob')

Now that we returned true the value of "Variable" is true. So lets say we wanted to do something if the conditions of a function based on the parameters given were true, we can define that function as a variable and create a condition. And example of this being seen here:

function FindSum(Value1, Value2)

    return (Value1 + Value2) -- Here we combine both values and return it

end

local Value = FindSum(1,2) -- Since the functions returns 1+2 the value of this variable is 3


if Value > 2 then -- since Value is now 3, it is greater than 2 so the condition is met 
    print("The value of variable is greater than 2")
end

I hoped my answer helped, it's been a really long time since I answered anything so I'm a bit rusty formatting

~Lone

Ad

Answer this question