|Introduction to Functions in Arduino C|
Web: Arduino: Functions
Reference: AVR Foundations. p. 45.
Many of the subtasks your code is required to perform is best developed in short, modular chunks, called functions (aka methods, subroutines). In this way, it is both easy to identify and repair errors as well as reuse the code in future projects.
Your mathematical understanding of a function, that returns a single value within the range of the function is a good place to start. The notation to the right is understood to mean that a value `x`, chosen from the eligible domain, is the input to the function. The expression defined by `f` replaces `x` with this input value and the expression is evaluated and returned to you as an element of the range, `y`.
loop() function headers are preceded by the
void keyword because they are intended to perform a task, not return a single value. If the purpose of a function is to perform a calculation on one or more input values, the function header would replace the
void keyword with another valid data type and use the
return statement to both exit the function and pass back the calculated result.
Functions in Arduino C, and (almost) any other other language, work in a similar manner. A call to a function can appear almost anywhere you would include a constant or a variable.
Example 1. Let's say in order to graph the function `f(x)=3x^2-1` in your notebook, you decide first create a table of values over the limited integer domain, [2,2]. You could easily do this by hand, but you want to try your hand at writing code to do it for you. You could write this.
Comments on Example 1
sprintfis optional, but preferred for formatted output usage.
yin line 16 and then simply return it in the next statement (although it supports clarity) This evaluation and exit could have been done in one
uint8_tfor both domain and range values. However...
floattype to allow for decimal places.
Example 2. Consider this improved version of Example 1...
Comments on Example 2.
forloops are simply consolidated
whileloops (initialization, terminal, and step in one line) and are encouraged for the iteration of ordinal sequences of integer or character types.
doubletypes) the formal
whilesyntax is preferred.
floattype due to the heavy demands it places on Flash memory. Consequently, I've shown you an alternative way to provide formatted support for
floattypes using the
dtostrf()function. Overkill perhaps, but this at least gives you control over the number of desired decimal places.
float(7 decimal places of accuracy). Within the Arduino C world, the
doubletype is the same as
y. The expression is evaluated and returned in a single statement.
Create an Arduino project called
Functions, and add functions to realize each of the following requirements. Be sure to consider the best return type and thoroughly test each function by displayed results to the Serial Monitor.
maximum, that accepts two
uint8_tparameters and returns the larger of the two.
maximumfunction that returns the larger of three
productthat accepts two
int8_tparameters and returns the product of the two.
discthat accepts the
cvalues of a quadratic expression `ax^2+bx+c` and returns the value of the discriminant, `b^2-4ac`.
dec2BCDthat accepts a decimal value from 0 to 99, inclusive and returns its BCD equivalent.
booleanresult. Develop the predicate method
uint16_tinput parameter is even and