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

I don't really get what returning does and I'm very confused with it?

Asked by 4 years ago
Edited 4 years ago

I'm way more advanced than this, but returning was always my weak spot. What exactly does it do? When are you supposed to use it? When I try to watch a video on it, they just use printing, I get it with printing but not other stuff. If possible please write a script using return that can use printing but just don't make it too simple.

1 answer

Log in to vote
4
Answered by
Psudar 882 Moderation Voter
4 years ago
Edited 4 years ago

It's actually a super simple concept.

When you return something in a function, that function just holds the value that it returned (Kinda like a variable would).

For example, if you did this:

local function Add() --Create the function
    return 2+2
end


local solution = Add() --Now we call it, it runs, and returns 2+2. That solution is 4. 


print(solution) 


-->>Output: 4

There are tons of other things you can do with returning. The basic idea though is that when you return something, the function holds that value. No code will run after something is returned.

For example:

local function func()
    print("Hey")
    return 
    print("Heyyy")
end

--Expected output: Hey

The second "Heyyy" wouldnt print, because the function ends at return. Im also pretty sure Lua would throw an error if you tried to run code after a return, but don't quote me on that.

I'm sure there's someone that can explain it a LOT better than I can internally, because I'm not that good at knowing what goes on under the hood of it all.

In conclusion, when you return something within a function, the function basically takes that values reference in its place, sorta like a variable. You can also use returns as a similar solution to using break with loops and such, since no code runs after you return something.

0
If you need any clarifications, let me know. I've always been bad at explaining stuff :p Psudar 882 — 4y
1
@Psudar well why and when would you use it? I get the idea of returning, its just when to use it bluemountainlion 56 — 4y
0
Its actually really useful. Nearly every default function in roblox returns something. For example, if you use :GetChildren(), that returns a table of all the children of the instance you used it on. Its really good when you want a function to calculate something (in the mathematical sense, as well as loosely meaning). Psudar 882 — 4y
0
If I had to recommend anything, just go in and mess around with returning. This was also something I struggled with understanding, as well as arguments/parameters. eventually it just clicked, and I got a lot better at programming because of it. Psudar 882 — 4y
View all comments (2 more)
1
An example of when to use a return thing for me would be to use a RemoteFunction to return data to see if a player had data or not. If they didn't have data, I would return "No data", and if they had data, I would return "Data". This determined if they could actually load a game or not ScrubSadmir 200 — 4y
1
Also a good use for it is to take local variables out a function Fad99 286 — 4y
Ad

Answer this question