Arduino Functions

 

Arduino Functions

Functions provide a way to modularize sketch and make it reusable.
Instead of having a single, very long program, you can break up your code into modules of the sketch with functions.

One advantage of using functions is that they avoid having to write the same code over and over again in a sketch which saves time and memory.
These functions perform certain, extending the kinds of things your sketch can do and can be used multiple times without having to re-write the same sketch.

To use functions, you have to:

         Define the function – enclose the statements of the function

         Call the function – use the function by using it’s name, adding any parameters if needed

        Use the result of the function - optionally, your code can do something with the result from the function

Example of  function definition.

void DashedLine()
{
    Serial.println("--");
}


components of a function


DashedLine= function name

Function  must be given a name.

  1. The function name will always be made up of alphanumeric characters (A to Z; a to z; 0 to 9) and the underscore (_).
  2. Name may not start with a number i.e. the numbers 0 to 9.
  3. Must not be used that is the same as a language keyword or existing function.

function will allways ends with parentheses ().

void = return type

A function must have a return type.

Function Body {    }=body

The function body is made up of one or more statements placed between braces {}. The statements make up the functionality of the function.

Calling a Function

a function must be called

i.e

void setup() {
  Serial.begin(9600);
  
  DashedLine(); // called here
  Serial.println("Hello Engineers");
  DashedLine(); /* Again caled here,  Every time that a function is called, 
we are just reusing code that has been written once. */ 
}

void loop() {
}

void DashedLine()
{
  Serial.println("------"); //is created here
}






Comments

Popular posts from this blog

Arduino function libraries~Math Library Function

Project Four: Pattern, Blink 4 leds using Arduino board

Project 6: Arduino print hello