Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:functions [2023/06/25 17:10] – created ktokarzen:iot-open:introductiontoembeddedprogramming2:cppfundamentals:functions [2023/11/23 10:20] (current) pczekalski
Line 1: Line 1:
-==== Sub-programs, functions ====+====== Sub-programs, Functions ====== 
 +{{:en:iot-open:czapka_b.png?50| General audience classification icon }}{{:en:iot-open:czapka_e.png?50| General audience classification icon }}\\ 
 +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, the program grows to a size that becomes hardly manageable as a single unit. It is quite difficult to navigate through the code that occupies many screensIn such a situation subprograms can helpSubprograms are named functions in C, and in C++ while they are associated with an object they are named 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. It is possible to group many functions creating the library, that is stored in a separate file. This is how external libraries are created.+==== Functions ==== 
 +Functions are the set of statements that are always executed when the function is calledA function can accept arguments as input data and return the resulting value. 
 +Two functions from the Arduino programming model 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.
  
-=== 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:
 </code> </code>
  
-In the example above can be seen that the return type of a//exampleFunction// function is //void// that means the function does not return any value. This function also does not have any arguments because the brackets are empty.+The example above shows that the return type of a//exampleFunction// function is //void//, which means the function does not return any value. This function also does not have any arguments because the brackets are empty.
  
 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:
 </code> </code>
  
-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 the loop()
   int result = sumOfTwoNumbers(2, 3);    int result = sumOfTwoNumbers(2, 3); 
 } }
 </code> </code>
  
 +==== Built-in functions ====
 +Every programming SDK, including Arduino IDE, comes with several ready-made functions that help develop applications, significantly reducing the effort and time of writing programs. These functions are written to handle inputs and outputs, process texts, communicate using serial ports, manipulate bits and bytes, and perform mathematical calculations. Refer to Arduino or other SDK documentation for details.
  
-**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. 
  
-1What are the built-in functions used for? +==== Function handlers ==== 
-  To reduce the size of the program+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 routinesThose functions are frequently called handlers and enable developers to inject their actions for a predefined set of activities without modifying library code. For this reason, the library contains a placeholder variable that can be assigned an executable code (a function body). This is handled with the use of pointers.  
-  * To delete unnecessary functions. +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: 
-  * To simplify the source file+<code c> 
-  * To increase the speed of the program.+int (*hUserImplementedFunction)(int); //Function handler variable  
 +                                      //(no code is here;  
 +                                      //it is just a pointer to the code,  
 +                                      //currently NULL, pointing to "nowhere" 
 +... 
 +int fMulx2(int a) {                   //User's implementation of the function.  
 +  return (2*a);                       //Multiply the argument 'a' value by 2 
 +                                      //and return it to the callee
 +  }                                   //Note: argument types and return types 
 +                                      //must match with the variable above 
 +...
  
-2. Which of the following statements is true? +hUserImplementedFunction = fMulx2;    //assign a function to the handler 
-  * Built-in functions must return a value. +                                      //starting from now,  
-  * Built-in functions cannot return values. +                                      //hUserImplementedFunction  
-  * The compiler can ignore the declaration of the built-in function. +                                      //contains an address of the fMulx2 function 
-  * Built-in functions cannot contain more than 10 lines of code.+..
 +int j;                                       
 +if (hUserImplementedFunction!=NULL)   //check if the handler is not null  
 +                                      //to avoid NULL pointer exception and code hang 
 +    j = hUserImplementedFunction(10); //call a handler, j is 20 now
  
-3. Is it possible to guarantee that the declared built-in function is really built-in? +</code> 
-  * Guarantee is not possiblein each case it is different. +<note important>In the example above, there is no "&" (address of) operator used when assigning a function code to the handler that is a pointer. It is because, by default, complex types as functions are referenced by reference (a pointer), not by value: ''fMulx2'' represents an address where the code starts.</note> 
-  * Can be confidently ensured that the function you have declared as built-in is really built-in. +<note tip>Using a function handler is common for asynchronous actionswhere user code is notified by the handler (usually low-level library code about the action to happen, e.g. data has been sent via the network interface). This method is similar to interrupts, as described later. Using function pointers (handlers) enables code to modify routines handling actions dynamically by substituting the addresses. Libraries frequently implement handler variables as lists or arrays instead of singular values, enabling adding more than one action (handler) to be called by the library.</note>
-  +
en/iot-open/introductiontoembeddedprogramming2/cppfundamentals/functions.1687713055.txt.gz · Last modified: 2023/06/25 14:10 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0