01.06.2013 Views

Manuel de KTurtle - KDE Documentation

Manuel de KTurtle - KDE Documentation

Manuel de KTurtle - KDE Documentation

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Manuel</strong> <strong>de</strong> <strong>KTurtle</strong><br />

4.6 Créez vos propres comman<strong>de</strong>s avec “apprends”<br />

apprends est une comman<strong>de</strong> très spéciale, car elle est utilisée pour créer vos propres comman<strong>de</strong>s.<br />

La comman<strong>de</strong> que vous créez peut recevoir <strong>de</strong>s entrées et retourner <strong>de</strong>s sorties. Regardons<br />

maintenant comment une nouvelle comman<strong>de</strong> est créée :<br />

apprends cercle $x {<br />

répète 36 {<br />

avance $x<br />

tournegauche 10<br />

}<br />

}<br />

La nouvelle comman<strong>de</strong> est appelée cercle. cercle reçoit un argument en entrée, qui fixe la<br />

taille du cercle. cercle ne retourne aucune sortie. La comman<strong>de</strong> cercle peut maintenant être<br />

utilisée comme une comman<strong>de</strong> normale dans la suite du programme. Voyez cet exemple :<br />

apprends cercle $X {<br />

répète 36 {<br />

avance $X<br />

tournegauche 10<br />

}<br />

}<br />

va 30 ,30<br />

cercle 20<br />

va 40 ,40<br />

cercle 50<br />

Dans l’exemple suivant, une comman<strong>de</strong> avec une valeur <strong>de</strong> retour est créée.<br />

apprends factorielle $x {<br />

$r = 1<br />

pour $i = 1 à $x {<br />

$r = $r * $i<br />

}<br />

retourne $r<br />

}<br />

écris factorielle 5<br />

Dans cet exemple, une nouvelle comman<strong>de</strong> appelée factorielle est créée. Si l’entrée <strong>de</strong> la<br />

comman<strong>de</strong> est 5, alors la sortie sera 5*4*3*2*1. En utilisant retourne, la valeur <strong>de</strong> sortie est<br />

spécifiée et le résultat est renvoyé.<br />

Les comman<strong>de</strong>s peuvent avoir plus d’une entrée. Dans l’exemple suivant, une comman<strong>de</strong> qui<br />

<strong>de</strong>ssine un rectangle est créé.<br />

apprends boîte $x , $y {<br />

avance $y<br />

tournedroite 90<br />

avance $x<br />

tournedroite 90<br />

avance $y<br />

tournedroite 90<br />

avance $x<br />

tournedroite 90<br />

}<br />

32

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!