Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Using a procedure's code to call itself
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
J-son




PostPosted: Thu Aug 28, 2014 5:16 pm   Post subject: Using a procedure's code to call itself

How does the logic work in a procedure calling itself?

Ex.

proc MainProgram (var Number : array 1 .. * of int)
.. snip ..
Main_Program (Lowest)
.. snip ..
end MainProgram

It doesn't seem to make sense to me but my code succeeds.
I don't understand how the procedure can call itself before it ends.

It sort of seems like once the computer reaches the procedure call it goes back up and starts running the procedure again and again..
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Thu Aug 28, 2014 6:58 pm   Post subject: RE:Using a procedure\'s code to call itself

Welcome to the lovely world of recursion.

Scroll down to you get to the recursion section.

http://compsci.ca/v3/viewtopic.php?t=14665


Edit:
As for how it works. Defining a function normally would be the equivalent of using forward procedure and body procedure. Forward is used to define the function's signature (the name of the function, how many arguments it has, and the data type of each of those arguments).

Turing:

forward function add(a: real, b: real)


Body is used to define the, well, body of a function.

Turing:

body function add(a: real, b: real)
    result a + b
end


So:

Turing:

function add(a: real, b: real)
    result a + b
end


is the same as writing:

Turing:

forward function add(a: real, b: real)
body function add(a: real, b: real)
    result a + b
end


Which means you can reference itself since it's already defined.
Raknarg




PostPosted: Thu Aug 28, 2014 9:27 pm   Post subject: RE:Using a procedure\'s code to call itself

Yup, as Zren said, this is known as recursion, and can be quite useful.

It's not any different from calling a function inside another function. The only difference is that here you're calling the same one over and over again.

For instance, try and figure out what this does:

Turing:

proc recursiveProcedure(n : int)
    if n <= 0 then
        put "End!"
    else
        put n
        recursiveProcedure(n-1)
    end if
end recursiveProcedure


What would happen, for instance, if we called recursiveProcedure(3) in our program?
J-son




PostPosted: Fri Aug 29, 2014 4:17 pm   Post subject: Re: Using a procedure's code to call itself

Thanks for the replies.

And I think that procedure would run for n=3, n=2 .. etc and display "End" when n=0.
evildaddy911




PostPosted: Tue Sep 02, 2014 9:37 am   Post subject: RE:Using a procedure\'s code to call itself

exactly. now see how useful and brain-melting that concept can be (as a matter of fact it confuses the hell out of my one teacher... quite funny to show him 8 lines and watch him sit for an hour trying to figure out how it works) i find it extremely useful in sorting and searching algorithms, where it makes it incredibly compact and efficient
Raknarg




PostPosted: Tue Sep 02, 2014 7:04 pm   Post subject: RE:Using a procedure\'s code to call itself

It can be quite inefficient for some problems though. For instance, the recursive solution for calculating Fibonacci numbers is impressively terrible:

Turing:

fcn fib(n : int) : int
    if n <= 1 then
        result 1
    else
        result fib(n-1) + fib(n-2)
    end if
end fib
evildaddy911




PostPosted: Fri Sep 05, 2014 8:28 pm   Post subject: Re: Using a procedure's code to call itself

since the original question was the logic behind recursion, what happens is exactly the same as calling a completely different procedure from within a procedure: it pauses the current procedure to run the new procedure. so say you had the following function:
code:
fcn foo (x :int) :int
   x := x - 1
   
   if x < 1 then
      result x
   end if

   var y : int
   y := foo (x)
   result y
end foo

what would happen if you called foo (4) is:

x = 3
x >= 1 so skip 'if' code
y =
{ % pause this function to open another
- - x = 2 % a new local variable that has nothing to do with the original x
- - x >= 1 so skip 'if' code
- - y =
{
- - - x = 1
- - - x >= 1 so skip 'if' code
- - - y =
{
- - - - x = 0
- - - - x < 1 so run 'if' code
- - - - - result 0
- - - }
y = 0
- - - result 0
- - }
y = 0
- - result 0
}
y = 0
result 0

(i didnt use code tags so that i could colour-code the different layers)
so as you can see, once the inner function closes, execution resumes from the point it left off until there are no more functions and it returns the final answer
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: