Objectifs

  1. A quoi sert une fonction ?
  2. Comment définir une fonction ?
  3. Comment utiliser une fonction ?
  4. Types des fonctions
  5. Exemples pratiques
  6. Etc.

Importance des fonctions

    1. Programmation modulaire
    2. Sclalable
    3. Facilité de débogage
    4. Simplification des taches répétitives
  1. Réduction de la complexité du code

Fonction sans retour (procédure)

void NomFunc(int a, int b, float c,...)

{

  instruction 1;

  instruction 2;

  instruction 3;

  ...;

}

Exemple

void Disp2Int(int a, int b)

{

  Serial.print("Valeur de a: ");

  Serial.println(a);

  Serial.print("Valeur de b: ");

  Serial.println(b);

  Serial.print("\n \n");

}

Fonction avec  retour

TypeRetour NomFunc(int a, int b, float c,...)

{

  TypeRetour Val;




  instruction 1;

  instruction 2;

  instruction 3;

  ...;




  return Val;

}

Exemple

int Sum2Int(int a, int b)

{

  int somme;

  somme=a+b;

  return somme; // Ou bien

  return a+b;

}

Fonction sans retour ni entrées

void NomFunc(void)



  instruction 1;

  instruction 2;

  instruction 3;

  ...;

}

Exemple

void VoidFun(void)

{

  Serial.println("Ici C'est sans arguments ni retour");

  Serial.println("wwww.electronique-mixte.fr");

  // Inst 2;

  // Inst 3;

  // ...;

}

Appel d’une fonction

int Somme;




int Sum2Int(int a, int b)

{

  int somme;

  somme=a+b;

  return somme; // Ou bien return a+b;

}




void Disp2Int(int a, int b)

{

  Serial.print("Valeur de a: ");

  Serial.println(a);

  Serial.print("Valeur de b: ");

  Serial.println(b);

  Serial.print("\n");

}




void VoidFun(void)

{

  Serial.println("Ici C'est sans arguments ni retour");

  Serial.println("wwww.electronique-mixte.fr");

  // Inst 2;

  // Inst 3;

  // ...;

}







void setup() {




  // Init port série

  Serial.begin (9600);

}




void loop() {




  // 1. Appel de la fonction sans retour 

  /*Disp2Int(10,20);

  delay(1000);*/




  // 2. Appel de la fonction avec retour

  /*Somme= Sum2Int(100, 10);

  Disp2Int(Somme,10);

  delay(1000); */




  // 3. Appel de la fonction sans retour ni antrées

  VoidFun();

  delay(1000);

}
void NomFunc(void)



  instruction 1;

  instruction 2;

  instruction 3;

  ...;

}




Exemple

void VoidFun(void)

{

  Serial.println("Ici C'est sans arguments ni retour");

  Serial.println("wwww.electronique-mixte.fr");

  // Inst 2;

  // Inst 3;

  // ...;

}




int Somme;




int Sum2Int(int a, int b)

{

  int somme;

  somme=a+b;

  return somme; // Ou bien return a+b;

}




void Disp2Int(int a, int b)

{

  Serial.print("Valeur de a: ");

  Serial.println(a);

  Serial.print("Valeur de b: ");

  Serial.println(b);

  Serial.print("\n");

}




void VoidFun(void)

{

  Serial.println("Ici C'est sans arguments ni retour");

  Serial.println("wwww.electronique-mixte.fr");

  // Inst 2;

  // Inst 3;

  // ...;

}




void setup() {




  // Init port série

  Serial.begin (9600);

}




void loop() {




  // 1. Appel de la fonction sans retour 

  /*Disp2Int(10,20);

  delay(1000);*/




  // 2. Appel de la fonction avec retour

  /*Somme= Sum2Int(100, 10);

  Disp2Int(Somme,10);

  delay(1000); */




  // 3. Appel de la fonction sans retour ni antrées

  VoidFun();

  delay(1000);

}
Click to rate this post!
[Total: 0 Average: 0]

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Retour en haut

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

FPGA | Arduino | Matlab | Cours will use the information you provide on this form to be in touch with you and to provide updates and marketing.