Functions

This is a programming exercise, in which we are going to show you the concept of functions.

!Explanation

A function is nothing more that a block of code that performs a series of predefined commands. The functions are largely used because they make reading the code easier and avoid that we repeat the same item a lot of times in our code.

An example is drinking water. This is a common and repetitive activity. Throughout the day we drink water several times. Somehow we do the same things every time we drink water: we get a glass (any kind of glass: small, medium, large), we take it to the water filter (metal filter, plastic filter, new, old, etc.), we open the faucet, put the water in the glass, close the faucet and drink.

If you had never drunk water, it’d be necessary to explain to you the step-by-step, specifying each part of the process and giving all the details. However, after the first time, I wouldn’t need to say: “get a glass, take it to the water filter, open the faucet, put water in the glass, close the faucet and drink”. I could simply say: drink water! And then you would already know how to do all by yourself. We call this message (“drink water!”) a function call.

!Parameters

The parameters of a function are the things that change in each function call. In the example above, I could say “drink water from a new filter with a small glass”. In this case, “new filter” and “small glass” are the function call parameters.

Below you can see how to use a function to turn a lamp on with a certain intensity (the parameter is the intensity).

!Declaring a function

Function: func( [parameters] ) { [consequence] };

A function has the following anatomy:

func([parameters])
{
  // Lines of code of the function func
}

!Function declaration example

Below there is an example of how to declare a function:

void light(int intensity)
{
  analogWrite(lamp, intensity);
  delay(500);
}

!Function usage example

In order to understand better, let’s rewrite the code from our last experiment using a function:

int lamp = 3;

// Function Declaration
void light(int intensity)
{
  analogWrite(lamp, intensity);
  delay(500);
}

void setup()
{
  pinMode(lamp, OUTPUT);
}

void loop() 
{
  // Function calls passing the values 
  light(5);
  light(55);
  light(105);
  light(155);
  light(205);
  light(255);
}

!Exercise

Try to rewrite the code of the Controlling the Buzzer project using functions.

Answer

// Pin connected to the buzzer
int buzzer = 5;

void ring_buzzer(int buzzer_pin, int frequency, int time_on)
{
  tone(buzzer_pin, frequency);
  delay(time_on);
  noTone(buzzer_pin);
}

void setup() 
{
  // Defines the Buzzer pin as Output
  pinMode(buzzer,OUTPUT);    
}

void loop() 
{
  // Makes the buzzer play the frequency relative to the musical note C in Hz
  ring_buzzer(buzzer, 261);    
  // Makes the buzzer play the frequency relative to the musical note D in Hz   
  ring_buzzer(buzzer, 293);             
  // Makes the buzzer play the frequency relative to the musical note E in Hz
  ring_buzzer(buzzer, 329);       
  // Makes the buzzer play the frequency relative to the musical note F in Hz
  ring_buzzer(buzzer, 349);    
  // Makes the buzzer play the frequency relative to the musical note G in Hz
  ring_buzzer(buzzer, 392);            
}