Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

“My Functions\" The ability to write functions is one of the more powerful capab

ID: 3592795 • Letter: #

Question

“My Functions" The ability to write functions is one of the more powerful capabilities in C. Functions allow code to be reused, and provides a mechanism for introducing the concept of modularity to the programming process. In addition to the concept of functions, C allows complex programs to be organized using header files (.h files) that contain the prototypes of functions and implementation files that (.cpp files) that contain the implementation (definition) of the functions. In this programming assignment you will be asked to implement a number of useful functions using a header file and an implementation file. You will place the prototypes for your function in a .h file, and implement these functions in a .c file. You will need to write a driver (a program designed to test programs) to test your functions before you upload them.

Deliverables:

• MyFunctions.h

• MyFunctions.c

• Driver.c

Function:

1. maximum

a Precondition

two double values exist

b Postcondition

The value of the larger double is returned

. The original doubles are unchanged

If the doubles have the same value then the value of either double is returned.

c Return

double

d Description

Function returns the value of the larger of two doubles.

e Prototype: double maximum (double, double);

2. minimum

a Precondition

two double values exist

b Postcondition

The value of the smaller double is returned.

The original doubles are unchanged

If the doubles have the same value then the value of either double is returned.

c Return

double

d Description

Function returns the value of the smaller of two doubles.

e Prototype: double minimum (double, double);

3. absolute

a Precondition

Some double value exists

b Postcondition

Double value is unchanged

c Return

Double d Description

This function returns the absolute value of a double

e Prototype: double absolute( double );

4. power

a Precondition

Double value X and integer value Y exist

b Postcondition

The value of X and Y are unchanged

c Return

The double X raised to the power of Y

d Description

This function will calculate

e Prototype: double power ( double, int );

5. squareRoot

a Precondition

Some double value X exists

NaN is defined in NaN.h

b Postcondition

The value of the radicand X is unchanged c Return

The square root of X NaN if an error is encountered

d Description

The function calculates the square root of a number using Newton’s method with 25 iterations.

e Prototype: double squareRoot( double );

Newton's method for calculating the square root of N starts by making a (positive number) guess at the square root. It then uses the original guess to calculate a new guess, according to the following formula: guess = (( N / guess) + guess) / 2 ; No matter how wild the original guess is, if we repeat this calculation the algorithm will eventually find the square root of N.

Explanation / Answer

Whilst the Lua standard distribution includes a stand-alone Lua interpreter, Lua itself is primarily an extension language that makes no assumptions about a "main" program: Lua works embedded in a host application to provide a powerful, lightweight scripting language for use within the application. This host application can then invoke functions to execute a piece of Lua code, can write and read Lua variables, and can register C functions to be called by Lua code. Through the use of C functions, Lua can be augmented to cope with a wide range of different domains, thus creating customized programming languages sharing a syntactical framework.

The ESP8266 was designed and is fabricated in China by Espressif Systems. Espressif have also developed and released a companion software development kit (SDK) to enable developers to build practical IoT applications for the ESP8266. The SDK is made freely available to developers in the form of binary libraries and SDK documentation. However this is in a closed format, with no developer access to the source files, so anyone developing ESP8266 applications must rely solely on the SDK API (and the somewhat Spartan SDK API documentation). (Note that for the ESP32, Espressif have moved to an open-source approach for its ESP-IDF.)

The NodeMCU Lua firmware is an ESP8266 application and must therefore be layered over the ESP8266 SDK. However, the hooks and features of Lua enable it to be seamlessly integrated without losing any of the standard Lua language features. The firmware has replaced some standard Lua modules that don't align well with the SDK structure with ESP8266-specific versions. For example, the standard io and os libraries don't work, but have been largely replaced by the NodeMCU node and file libraries. The debug and math libraries have also been omitted to reduce the runtime footprint (modulo can be done via %, power via ^).

NodeMCU Lua is based on eLua, a fully featured implementation of Lua 5.1 that has been optimized for embedded system development and execution to provide a scripting framework that can be used to deliver useful applications within the limited RAM and Flash memory resources of embedded processors such as the ESP8266. One of the main changes introduced in the eLua fork is to use read-only tables and constants wherever practical for library modules. On a typical build this approach reduces the RAM footprint by some 20-25KB and this makes a Lua implementation for the ESP8266 feasible. This technique is called LTR and this is documented in detail in an eLua technical paper: Lua Tiny RAM.

The main impacts of the ESP8266 SDK and together with its hardware resource limitations are not in the Lua language implementation itself, but in how application programmers must approach developing and structuring their applications. As discussed in detail below, the SDK is non-preemptive and event driven. Tasks can be associated with given events by using the SDK API to registering callback functions to the corresponding events. Events are queued internally within the SDK, and it then calls the associated tasks one at a time, with each task returning control to the SDK on completion. The SDK states that if any tasks run for more than 15 mSec, then services such as WiFi can fail.