Caesar Cipher in a Function
Why move the Caesar cipher routine to a function? One advantage would be that you can swap it out with other, better encryption functions, or even function/method calls to a module. As an example, in the next activity, you will replace the caesar function with another one called ascii_shift. After the function swap, your project will only need one block changed!
Example project: caesar_cipher_function
- Enter caesar_cipher_function, then flash it into the micro:bit.
The functionality is identical to the previous example (caesar_terminal_words).
- Open the terminal, check that local echo is checked, follow the prompts, and verify that the functionality matches the previous project.
- Take a look at the actual project you have to work with, it’s the parts inside both the start here block and the forever loop.
See how all that project has to do is use call caesar to encrypt the word?
How It Works
Your project now gets the ciphertext from this one block:
The caesar function has two parameters, key and word. Keep in mind that word could also be ciphertext that you are converting back with a negative key value. Inside the function, it does all the same steps as caesar_terminal_words, and then returns the result.