Type Here to Get Search Results !

How To Use Custom Function In Arduino Programming

We saw two functions, "setup" and "loop" that are part of any sketch program in general we can write all our program in those two functions and everything will just ok. However, when our project is becoming more complicated and there more lines of code that are needed than putting everything in the loop function is making our program unreadable, very hard to debug and less useable for re-use in other projects. So in programming it is common practice to divide big tasks to smaller tasks using functions.



A function is a block of code that perform a specific task  that is repeating in program now instead of writing the same code all over again, we can just call the function name.

Let’s how we define a new function, it is very easy starting at the beginning, we specify the function’s data type, in this case "integer", followed by the function’s name, myfunction, parameters we may or may not pass to the function curly braces required to mark the function body and of course the block of code. That's doing something....

In this specific case, this function is getting to value x and y, calculate the sum and return that as a result. So if you going to the loop function, above you can see that we defined. Some parameter that is called "sum" and we call the "myfunction" with the value of 4 and 27 and of course this function will calculate that and return the value 31 and put it in the integer "sum".

That's all, this is an example of creating custom function, very useful while creating sketch. Please watch this video to understand better.



Post a Comment

0 Comments