This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:functions [2023/07/10 21:52] – pczekalski | 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 always executed 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 usually tries to make several functions containing all the statements and then calls 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 78: | Line 78: | ||
</ | </ | ||
- | === Built-in functions === | + | ==== Built-in functions |
Every programming SDK, including Arduino IDE, comes with several ready-made functions that help develop applications, | Every programming SDK, including Arduino IDE, comes with several ready-made functions that help develop applications, | ||
- | === Library functions === | + | ==== Library functions |
- | The popularity of microcontrollers and embedded programming caused the growth of communities of enthusiasts who create a vast of useful | + | The popularity of microcontrollers and embedded programming caused the growth of communities of enthusiasts who create a vast of helpful |
- | === Function handlers === | + | ==== Function handlers |
- | 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 are frequently called handlers and enable developers to inject their actions for a predefined set of activities without | + | 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 are frequently called handlers and enable developers to inject their actions for a predefined set of activities without |
A sample function handler variable is presented in the following code, along with the user function definition, assignment to the handler variable and a call to the handler: | A sample function handler variable is presented in the following code, along with the user function definition, assignment to the handler variable and a call to the handler: | ||
<code c> | <code c> | ||
- | int (*hUserImplementedFunction)(int); | + | int (*hUserImplementedFunction)(int); |
- | //(no code is here; it is just a pointer to the code, | + | //(no code is here; |
- | //currently NULL, pointing to " | + | //it is just a pointer to the code, |
+ | //currently NULL, pointing to " | ||
... | ... | ||
- | int fMulx2(int a) { | + | int fMulx2(int a) { |
- | return (2*a); | + | return (2*a); |
- | } | + | //and return it to the callee. |
+ | } | ||
+ | //must match with the variable above | ||
... | ... | ||
- | hUserImplementedFunction = fMulx2; | + | |
- | //starting from now, hUserImplementedFunction contains an address of the | + | hUserImplementedFunction = fMulx2; |
- | //fMulx2 function | + | //starting from now, |
+ | //hUserImplementedFunction | ||
+ | //contains an address of the fMulx2 function | ||
... | ... | ||
int j; | int j; | ||
- | if (hUserImplementedFunction!=NULL) | + | if (hUserImplementedFunction!=NULL) |
- | j = hUserImplementedFunction(10); | + | //to avoid NULL pointer exception and code hang |
+ | j = hUserImplementedFunction(10); | ||
</ | </ | ||
- | <note important> | + | <note important> |
- | <note tip> | + | <note tip> |
- | < | + |