\b;Les fonctions Lorsqu'un programme devient long, il est conseillé de le fragmenter en plusieurs fonctions, qui accompliront chacune une tâche bien précise. Imaginons le programe suivant: \c; \s;extern void object::Remote( ) \s;{ \s; send("order", 1, 100); \s; wait(5); \s; send("order", 3, 100); \s; wait(5); \s; send("order", 2, 100); \s; wait(5); \s; send("order", 4, 100); \s; wait(5); \s;} \n; On constate que les deux instructions \c;send\n; et \c;wait\n; sont répétées plusieurs fois. Il est donc judicieux de créer une fonction \c;SendToPost\n; qui effectue ces deux instructions: \c; \s;extern void object::Remote( ) \s;{ \s; SendToPost(1); \s; SendToPost(3); \s; SendToPost(2); \s; SendToPost(4); \s;} \s;void object::SendToPost( float op ) \s;{ \s; send("order", op, 100); \s; wait(5); \s;} \n; Une fonction peut recevoir des données en entrée. Il faut en donner la liste, avec à chaque fois le type de la variable et le nom qui lui est donné: \c; \s;void Exemple( int a, float x, string s ) \n; La fonction \c;Exemple\n; va recevoir un nombre entier \c;a\n;, un nombre réel \c;x\n; et une chaîne \c;s\n;. Parameters are "passed by value", that is the values of parameter variables in a function are copies of the values the caller specified as variables. If you pass an \c;int\n; to a function, its parameter is a copy of whatever value was being passed as argument, and the function can change its parameter value without affecting values in the code that invoked the function. Les \l;tableaux\u cbot\array; et les instances de \l;classes\u cbot\class; sont toujours passées par \l;référence\u cbot\pointer;. That means if you modify the instance or the array in the function, the instance or the array that has been specified by the caller will be actuallay modified. Une fonction peut effectuer un calcul et retourner le résultat avec l'instruction \c;\l;return\u cbot\return;\n;. Therefore the function must be declared no longer as void but as a type: \c; \s;float Moyenne( float a, float b ) \s;{ \s; return (a+b)/2; \s;} \s;extern void object::Essai( ) \s;{ \s; float value; \s; value = Moyenne(2, 6); \s; message( value ); // affiche 4 \s;} \n; Voici d'autres exemples de fonctions: \c; \s;float Pi( ) \s;{ \s; return 3.1415; \s;} \s; \s;string Signe( float a ) \s;{ \s; if ( a > 0 ) return "positif"; \s; if ( a < 0 ) return "négatif"; \s; return "nul"; \s;} \n; Il est autorisé de créer plusieurs fonctions ayant le même nom mais des paramètres différents: \c; \s;float Pythagore( float a, float b ) \s;{ \s; return sqrt((a*a)+(b*b)); \s;} \s; \s;float Pythagore( float a, float b, float c ) \s;{ \s; return sqrt((a*a)+(b*b)+(c*c)); \s;} \n; Lors de l'appel à la fonction, CBOT recherche la fonction dont les paramètres correspondent au mieux. You can also declare a function \l;public\u cbot\public; so it can be used by other bots. \t;Voir aussi \l;Programmation\u cbot;, \l;types\u cbot\type; et \l;catégories\u cbot\category;.