Tic how to use recursion on karel

Solo disponible en BuenasTareas
  • Páginas : 2 (252 palabras )
  • Descarga(s) : 0
  • Publicado : 4 de septiembre de 2012
Leer documento completo
Vista previa del texto
Act 7
How recursion works using a proposed example


Recursive is a function when it invokes itself from some point, obviously from inside its code. When a recursive function is invoked for thefirst time, Karel loads into memory a copy of its code, as any other function, the interesting starts when, based on the evaluation of a “condition” inside of a “if(condition)” occurs the called ofinvoking to the same function.
Well this process will be repeated while the “condition” is fulfilled, from which it is doing the recursive calling; this is, the fact of calling itself, to load othercopy of the recursive function and leave on standing by the current copy of the new function.

Demonstration of how the recursion works.
| void MoveandPutBeeper() { |
|if(anyBeepersInBeeperBag ()) { |
|putbeeper(); |
|MoveandPutBeeper(); |
|move(); |
|}|
|} |


Next there is the code for this example, we will use a recursive function with the name “MoveandPutBeeper()”. But before, let us see animage of the copies of the function “MoveandPutBeeper()”, that Karel loads into memory per earch recursive calling that occurs in this process.























Testing:Initial World Final World











Program:
class program {

void MoveandPutBeeper() {
if(anyBeepersInBeeperBag ()) {
putbeeper();
MoveandPutBeeper();
move();
}
}

program() {
MoveandPutBeeper();
;
turnoff();
}
}
Conclusion:
Recursionis very interesting and also pretty important, If you don’t want to spend time to write a lof of commands, you should use recursion, it invokes itself from some point, from inside its code.
In the...
tracking img