generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Recursion tutorial

Previous Thread :: Next Thread 
miz656 is not online. miz656
Joined: 19 Jul 2010
Total Posts: 15336
20 May 2012 09:28 PM
You know? I've never seen in my time I've been in roblox one person having a program using recursion. So I though, you might not know what it is or you don't get it. So I shall be thinking I should teach you it.

Before I start if you don't get this tutorial then go here for the article about it.

http://wiki.roblox.com/index.php/Recursion

Now, time to start.

"What's recursion?" You may ask. It's calling your function inside your function. It's like looping until your base, or your condition is true.


So, lets get started. I'm going to make a simple function called x.

function x(blah)
print(blah)
end
x("cheese")

This will print cheese right? Because I used the parameter, blah, as my arguement in print. Then I called it saying x("cheese"). I just wanted to make that clear if you don't know what a function is.

Now, let me say my recursive program.

function x(equat)
if equat >=5 then
return 1
elseif equat < 5 then
return equat * x(equat + 1)
end
end
print(x(1))

--24

"How did you get 24?" You might be concerned. Here, let me explain that to you.

Our function is called x, and it takes one arguement. equat. Now, you see that if statement? Yeah, that's our base. We need that to check when the recursion stops. "What happens if we don't have it?" You might think. Well, if we don't have that if statement then this whole code will error making a stack overflow. What I mean by that is it will go on forever and ever and ever calling the function forever. That's why it will error.

So, as you see it checks if equat is greater than 5. As you see, that's false because I called it 1. So it checks the elseif condition. Yes, since 1 is less than 5 it runs that part of the code. So, here comes the recursive part.

"return equat * x(equat + 1)"

This is the hard part to explain, so let me try my best to explain it.

Let me get my jacket, ok? Say my jacket has five pockets. Why five pockets? Because it shows how many times I use recursion since the if statement says greater or equal to five.

So, return equat is our first value. So I'm going to put that in pocket one. It's the first value because I called it in the print, making it 1. So in my first pocket in my jacket is 1.

Then x(equat +1) goes in my second pocket. It says add 1 more to the last value and go all the way from the beginning since I call it in this code. So value 2 goes in my second pocket.

Now, since it goes back to the code, it checks again to see if equat is >=5. Is it? No. Because not all my pockets are greater than 5. So not all my pockets are filled. So it goes again
"return equat * x(equat + 1)

So what's 2 + 1? 3, so now my third pocket is full with the value 3.

Again, 3 is not greater than five, so it goes to the elseif statement. 3 + 1 more is 4. So my fourth value is filled with pocket four.

Now, finally, you may be thinking "eppp, go to the elseif statement because 4 is not greater than 5." But look at the next line. It says "return 1" This is our fifth value showing that our fifth value is 1. Now, since we have five values we can print now.

Let me go back to my pockets and see what I have.

First pocket: 1
second pocket : 2
third pocket :3
fourth pocket :4
fifth pocket :1

What's 1 * 2 * 3 * 4 * 1? 24, yep.

I hoped this help really well. The reason I did this is because

1. Wasn't sure if you guys knew since I've never seen someone talk about recursion.
2. There are many languages that take recursion, so it's better to learn early.

And if this didn't work, try the wiki.
http://wiki.roblox.com/index.php/Recursion

Wish you good luck!
Miz656
Report Abuse
crazyman32 is not online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
20 May 2012 09:31 PM
function hi(s)
s = s.."hi"
print(hi)
wait()
hi(s)
end

hi("hi")
Report Abuse
swimguy777 is not online. swimguy777
Joined: 30 May 2009
Total Posts: 17092
20 May 2012 09:33 PM
function a()
a()
end
a()

-[::ƧѡÎḾḠΰῩ::]-[::Maker of stuff and Helper of Scripting::]-
Report Abuse
miz656 is not online. miz656
Joined: 19 Jul 2010
Total Posts: 15336
20 May 2012 09:34 PM
So now I see recursion? :/
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
20 May 2012 09:38 PM
"I've never seen in my time I've been in roblox one person having a program using recursion."
You have a model of my maze generator ._.

http://www.roblox.com/Smurfs-Maze-Generator-V-2-item?id=68919014
Report Abuse
rayoma is not online. rayoma
Joined: 13 Nov 2009
Total Posts: 1911
20 May 2012 09:45 PM
What? Almost all of my games are based heavily on recursion? I don't see how you haven't seen people using recursion miz.
Report Abuse
crazyman32 is not online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
20 May 2012 09:52 PM
Oh, and I use recursion all the time. Especially when scanning models for things.
Report Abuse
rayoma is not online. rayoma
Joined: 13 Nov 2009
Total Posts: 1911
20 May 2012 09:55 PM
And even with the sumobot tournament YOU entered, the weight script I made uses recursion.

    function weight(model)
        local w = 0
        local function search(obj)
            for _, v in pairs(obj:GetChildren()) do
                if v:IsA("BasePart") then
                    w=w+v:GetMass();
                end
                search(v);
        end end
        search(model)
        return math.floor((w * 10) + 0.5) / 10
    end
Report Abuse
miz656 is not online. miz656
Joined: 19 Jul 2010
Total Posts: 15336
20 May 2012 09:57 PM
Smurf

I just looked by the metatables.

Crazyman

I've never seen you or talk on the forum about recursion

rayoma

You mostly update your football game :/
Report Abuse
damanofrunescape is not online. damanofrunescape
Joined: 29 Jul 2008
Total Posts: 7338
20 May 2012 09:57 PM
And there was recursion used often when raycasting (to ignore more things).
Report Abuse
miz656 is not online. miz656
Joined: 19 Jul 2010
Total Posts: 15336
20 May 2012 09:58 PM
Oh yeah. . . Derp.

But I never see it on the scripting forum, so I'm guessing these guys don't know it.
Report Abuse
rayoma is not online. rayoma
Joined: 13 Nov 2009
Total Posts: 1911
20 May 2012 09:58 PM
Because people don't specifically ask about it however, we do use it here.
Report Abuse
smurf279 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
20 May 2012 09:58 PM
My maze gen doesn't use metatables ._.
Report Abuse
crazyman32 is not online. crazyman32
Joined: 13 Apr 2008
Total Posts: 18027
20 May 2012 10:00 PM
@miz656 - I haven't posted anything about them because I haven't ran into a time where I needed to use them as an explanation for something.

I've been using them widely for probably 2 years. Very commonly I use them simply to collect parts:


local all = {}
function scan(p)
for _,v in pairs(p:GetChildren()) do
if (v:IsA("BasePart")) then table.insert(all,v) end
scan(v)
end
end
scan(OBJECT)
Report Abuse
miz656 is not online. miz656
Joined: 19 Jul 2010
Total Posts: 15336
20 May 2012 10:01 PM
. . .

What?

Report Abuse
miz656 is not online. miz656
Joined: 19 Jul 2010
Total Posts: 15336
21 May 2012 08:38 PM
Bump.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image