Site icon AranaCorp

Summary of function definition in C

0
(0)

In programming, it is very common or even necessary to use functions that require special definition. This is especially true when code blocks are repeated in the same program. A set of functions and objects are usually collected in libraries. We will see in this tutorial the C function definition applied to Arduino.

Syntax for function definition in C

The syntax of the definition of a C function consists of 4 parts:

.

bool nom_fonction(int arg1, char arg2){
  bool out; //definition variable locale
  //corps de la fonction
  return out;
}

The type of the function, its name and the definition of the arguments correspond to the prototype of the function, i.e. the minimum information to be known in order to call the function.

resultat = nom_fonction(param1,param2);

Examples

int convert(int val){
  return map(val,0,1023,-100,100);
}


bool isValid(int val){
  if(val>-5 && val<50){
    return true; 
  }else{
    return false;
  }
}


int increment(int val, int increment,int maxval, int minval){
  val+=increment;
  if(val>maxval) val=minval;
  if(val<minval) val=maxval;

  return val;
}


A function which does not return a value is called a procedure, it is characterized by a void type.

int myVar=0;

void setup(){
  Serial.begin(115200);
}

void loop(){
   printVar("myVar",myVar);
   myVar+=1;
   delay(500);
}

void printVar(String nom, int val){
  Serial.print(F(">> "));Serial.print(nom);Serial.print(F(": "));Serial.println(val); 
}

>> myVar: 0
>> myVar: 1
>> myVar: 2
>> myVar: 3
>> myVar: 4
>> myVar: 5
>> myVar: 6
>> myVar: 7
>> myVar: 8
>> myVar: 9
>> myVar: 10

Important to remember about the definition of function in C

Bonus

Defining arguments with default values

A default value of one or more parameters can be set when defining a function. Under Arduino, to be able to compile such a function, you will have to add the prototype of the function (or the whole definition) before the first use of the function.

At the beginning of the file, we define the prototype of the function by specifying the default value.

void printVar(String nom, int val, String unit="");

Then, following or at another place, we add the definition of the function without the default values

void printVar(String nom, int val, String unit){
  Serial.print(F(">> "));Serial.print(nom);Serial.print(F(": "));Serial.print(val);
  Serial.print(F(" "));Serial.print(unit);
  Serial.println();
}

It is then possible to use the function with or without the parameter in order to obtain different behaviours.

   printVar("myVar",myVar);
   printVar("cmVar",cmVar,"cm");

int myVar=0;
int cmVar=0;

void printVar(String nom, int val, String unit="");

void setup(){
  Serial.begin(115200);
}

void loop(){
   printVar("myVar",myVar);
   printVar("cmVar",cmVar,"cm");
   
   myVar+=1;
   cmVar+=5;
   delay(1000);
}

void printVar(String nom, int val, String unit){
  Serial.print(F(">> "));Serial.print(nom);Serial.print(F(": "));Serial.print(val);
  Serial.print(F(" "));Serial.print(unit);
  Serial.println();
}

Function overload

It is possible to define several prototypes so that a function with the same name can act on parameters of different types. To override a function, we need to declare the function for each list of desired parameters with different prototypes and bodies

N.B.: Please note that the function overload is only possible with different argument lists.

int myVar=0;
int cmVar=0;

void printVar(String nom, int val, String unit="");
void printVar(String nom, String mot, String unit="'");

void setup(){
  Serial.begin(115200);
}

void loop(){
   printVar("myVar",myVar);
   printVar("cmVar",cmVar,"cm");
   printVar("cmVar","HelloWorld");
   myVar+=1;
   cmVar+=5;
   delay(1000);
}

void printVar(String nom, int val, String unit){
  Serial.print(F(">> "));Serial.print(nom);Serial.print(F(": "));Serial.print(val);
  Serial.print(F(" "));Serial.print(unit);
  Serial.println();
}

void printVar(String nom, String mot,String unit){
  Serial.print(F(">> "));Serial.print(nom);Serial.print(F(": "));Serial.print(unit);Serial.print(mot);
  Serial.print(unit);
  Serial.println();
}

>> myVar: 3 
>> cmVar: 15 cm
>> Text -> 'HelloWorld'
>> myVar: 4 
>> cmVar: 20 cm
>> Text -> 'HelloWorld'
>> myVar: 5 
>> cmVar: 25 cm
>> Text -> 'HelloWorld'
>> myVar: 6 
>> cmVar: 30 cm
>> Text -> 'HelloWorld'
>> myVar: 7 
>> cmVar: 35 cm
>> Text -> 'HelloWorld'

Sources

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Exit mobile version