Skip to content

Arduino Functions

    A function is a closed unit within a program and is called by its name. Usually with the function call still values are passed, which are processed then within the function. These values are called “parameters”. After execution of the function, which can be a calculation, a conversion or a signal output to an output port, this “small program” is executed and the main program, which called the function, is continued.

    Often, however, the called function returns a response (called function return). This can be the result of a calculation or a status of a state.

    Function Structure

    The structure of the function is as follows:

    Type FunctionName (type parameter){
    //Function body
    rerun val;

    An example looks like this:

    Function Structure

    The type of a function indicates what kind of value is returned from the function. This can be int, char, etc. Accordingly, the data type of this return value is specified in the call of the function before the function name. The call of a function is usually made from the main program.

    Arduino Function Example

    Arduino sensorRead program

    Program 1 shows the call of the function sensorRead from the main program (loop). The return value is the analog value with the int data type. For a function that does not have a return value, the keyword void is used for the type designation.

    Program 2

    In Program 2, the loop calls the function sensorReadPrintSerial which has no return value and no input parameter. Consequently, this it calls another function printVal, which has an input parameter an no return.