-
What do you want to achieve? I want to learn about returning. I have tried searching it up on youtube but no luck. Can you help?
-
What solutions have you tried so far? Youtube
you mean the return that was used in RemoteFunctions?
I’ll take an example:
function CreatePart() -- a function for creating a part
local Part = Instance.new("Part", workspace) -- creates a part first
return Part -- when this function is used, it will returns the part
end
local Part = CreatePart() -- this is a part that was returned from the function
print(Part.Name) -- expected output: Part, which is the part's name
Returning is not specific to RemoteFunctions; any function can use them. You’re not even using RemoteFunctions in your example, so I’m not sure why you brought them up.
(For the OP:)
Returning is how a function gives an “output.” Think of a function as a rule from way back when in mathematics:
Or a function from mathematics, but that’s direct enough that it’s not much of an analogy.
The input is essentially the arguments passed to the function.
You can call a function and give it an input using arguments. These arguments will be available to the function as the parameters specified when you defined the function. For example, you can define a function like this:
local function x(y, z) -- y and z are "parameters"
print(y, z)
end
… and calling it with up to two values will print those values. You can try this out:
-- function definition from above
x(1, 2) --> 1, 2 is printed, 1 and 2 are "arguments"
x(3, 4) --> 3, 4 is printed, 3 and 4 are "arguments"
If a function is like a “rule” in math, though, those rules usually “transform” (change) the input and give back a new number. How do functions give back the new number? Through the return
keyword. Let’s take a basic rule, x + 4. We can define a function that takes a number:
local function ourRule(x)
end
To add 4, we already know we can just do whatever + 4
, obviously.
local function ourRule(x)
local y = x + 4
end
That variable will just exist in the function, though, since it’s a local variable. We want it back where we called the function, which is where return comes in. We can just put return
before a value to get it back to wherever the function is called. We can show this by doing something like this:
local function ourRule(x)
return x + 4 -- the return keyword can take any number of values, too, not just 1
end
local y = ourRule(5)
print(y) --> 9, aka 5 + 4
This, along with parameters, lets us reuse a function for multiple purposes. For example, we can obviously do the math thing, and call ourRule with however many inputs to figure out the input and output across a specified domain. That’s not very practical, though, since we could just write local y = 5 + 4
instead of local y = ourRule(5)
, or even just local y = 9
. Where this really comes in handy is with more complicated code.
An important tenet of more advanced coding is DRY, or Don’t Repeat Yourself. While it might be hard to achieve for a beginner, the concepts are simple; reuse code where possible instead of copy-pasting the same stuff. If you do the same thing to several parts, it’s better to loop through the parts or pass them each to a function than to do the same thing 20 times with part1, part2, and so on. It makes intuitive sense, too; if you can make the computer work out how to do the same thing to all those different parts, let it do the heavy lifting!