Chapter 8 - Basic Programming construct - (iv) Functions

Basic Programing Construct (ii): Functions

Functions are yet another construct found in all types of programming languages.

Functions allow the coder to name useful chunks of code so that they can use them repeatedly without having to type the whole thing out each time.

  Believe it or not? Functions are nothing new & we have used functions before this chapter.
e.g. serial.print("Text");

ReturnType FunctionName (ArgumentType Argument) {
    Statements...;
}





Benefits of using Functions:
- It helps you (Programmer) stay organized, especially when working as a team. In addition, it is easier to maintain the codebase.
- Modifying in one location reduces the chances of error. And invoking function allows you to quickly test out changes without much impact.
- Reducing similar code will ultimately lead to a smaller sketch output file (firmware).
- Promote sharing & reusing of code in another project.



Special functions - Operators

We have seen Comparison as well as Boolean operators in our previous chapter. Now we are going to discuss yet another type of operator. Arithmetic operator.

Operator Symbol Operator Name Description
= Assignment operator Stores the value to the right of the equal sign in the variable to the left of the equal sign.
+ Addition Adds two operands.
- Subtraction Subtracts second operand from the first.
* Multiplication Multiply both operands.
/ Division Divide first operand (numerator) by the second operand (denominator).
% Modulo Modulus Operator and remainder after an integer division.

 All the Arithmetic operator symbol comes as no surprises. except for Multiplication, Division & Modulo.



 Function example - Morse Code Encoder:

An early form of Serial communication actually came in a form of morse code. Using a single channel (Light source or a Beeping sound), messages could be encoded & decoded on both ends of the communication.

Being good in morse code is not a trivial matter & it takes hours or months of practice to be good at it. With technology on our hands, we will implement an instant encoder to instantly convert text keyed in through your keyboard as a sequence of light flashes.



You could read up more about Morse code from the following wikipedia page.
Wiki:  https://en.wikipedia.org/wiki/Morse_code

 Todo:

  • I am pretty sure you have heard of encryption before. An early form of encryption named after Julius Caesar is the Caesar cipher. It is one of the simplest and most widely known encryption techniques.

    To encrypt a line of text, each letter in the plaintext is replaced by a letter with some fixed number (communicated beforehand) of positions down the alphabet list.



    Shown above is the tool with 2 rotating disk to enable quick encrypting & decrypting of a character.

    Implement a software that could encrypt & decrypt a message with the CoreModule. You can verify your result with any Caesar cipher you can find online.
     https://www.boxentriq.com/code-breaking/caesar-cipher