This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:functions [2023/06/25 17:10] – created ktokarz | en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:functions [2023/11/23 10:20] (current) – pczekalski | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ==== Sub-programs, | + | ====== Sub-programs, |
+ | {{: | ||
+ | In many cases, the program grows to a size that becomes hardly manageable as a single unit. It isn't easy to navigate through the code that occupies many screens. In such a situation, subprograms can help. Subprograms are named functions in C and C++; while they are associated with an object, they are called methods (in this chapter, the name // function // will be used). The function contains a set of statements that usually form some logical part of the code that can be separately tested and verified, making the whole program easy to manage. Grouping many functions by creating a library stored in a separate file is possible. This is how external libraries are constructed. | ||
- | In many cases, | + | ==== Functions ==== |
+ | Functions are the set of statements | ||
+ | Two functions | ||
- | === Functions === | + | The structure of the function is as follows: |
- | Functions are the set of statements that are executed always when the function is called. A function can accept arguments as its input data, and return the resulting value. | + | |
- | Two functions from the Arduino programming model that were mentioned before are already known – //setup()// and //loop()//. The programmer is usually trying to make several functions that contain all the statements and then call them in the //setup()// or //loop()// functions. | + | |
- | + | ||
- | The structure of the function is following: | + | |
<code c> | <code c> | ||
type functionName(arguments) //A return type, name, and arguments of the function | type functionName(arguments) //A return type, name, and arguments of the function | ||
Line 27: | Line 27: | ||
</ | </ | ||
- | In the example above can be seen that the return type of a// | + | The example above shows that the return type of a// |
This function should be called inside the //loop()// function in the following way: | This function should be called inside the //loop()// function in the following way: | ||
Line 69: | Line 69: | ||
</ | </ | ||
- | In the //loop()// this function would be called in the following way: | + | In the //loop()//, this function would be called in the following way: |
<code c> | <code c> | ||
void loop() | void loop() | ||
{ | { | ||
- | //the call of the defined function inside loop() | + | //the call of the defined function inside |
int result = sumOfTwoNumbers(2, | int result = sumOfTwoNumbers(2, | ||
} | } | ||
</ | </ | ||
+ | ==== Built-in functions ==== | ||
+ | Every programming SDK, including Arduino IDE, comes with several ready-made functions that help develop applications, | ||
- | **Check Yourself** | + | ==== Library functions ==== |
+ | The popularity of microcontrollers and embedded programming caused the growth of communities of enthusiasts who create a vast of helpful software. This usually comes as a set of functions designed to handle specific tasks, e.g. interfacing with a family of graphical displays or communicating using the chosen protocol. Functions created for one purpose are grouped, forming the library. The number of libraries and their different version is so significant that software developers use a particular library manager to ensure that libraries are up-to-date or keep them in stable versions. | ||
- | 1. What are the built-in functions used for? | + | ==== Function handlers ==== |
- | * To reduce | + | In the MCU world, is is common to use libraries that require a user (software developer) to implement a specific part of the code that is later automatically called by the library routines. Those functions |
- | * To delete unnecessary functions. | + | A sample function handler variable is presented |
- | * To simplify | + | <code c> |
- | | + | int (*hUserImplementedFunction)(int); |
+ | //(no code is here; | ||
+ | //it is just a pointer to the code, | ||
+ | //currently NULL, pointing to " | ||
+ | ... | ||
+ | int fMulx2(int a) { // | ||
+ | | ||
+ | //and return it to the callee. | ||
+ | | ||
+ | //must match with the variable above | ||
+ | ... | ||
- | 2. Which of the following statements is true? | + | hUserImplementedFunction = fMulx2; |
- | * Built-in functions must return a value. | + | // |
- | * Built-in functions cannot return values. | + | // |
- | * The compiler can ignore the declaration | + | // |
- | * Built-in functions cannot contain more than 10 lines of code. | + | ... |
+ | int j; | ||
+ | if (hUserImplementedFunction!=NULL) | ||
+ | //to avoid NULL pointer exception and code hang | ||
+ | j = hUserImplementedFunction(10); | ||
- | 3. Is it possible to guarantee that the declared built-in | + | </ |
- | * Guarantee | + | <note important> |
- | * Can be confidently ensured that the function | + | <note tip> |
- | | + |