Leaf functions

In Session 8 you learned how to realize simple algorithms. But you also saw that we need functions.

What are functions?

It's an implementation pattern to avoid the mess you had to deal with in Session 8. We had code for reading in an integer, code for printing an integer and code for computing the factorial. You managed it to put things together in a long and messy chain of instructions. In a simplified way, that could be described like that:

Its simplified because each of this boxes represents more than just a single instruction, these were blocks of code. Two of these code blocks were basically doing the same thing, printing some string, but you had to write it twice (or “copy and paste” it, adjust it with “search and replace”). It was also a mess because you had to make sure the code blocks are not in conflict with each other (by using different labels, and by figuring out what registers are free to use).

With (leaf) functions we can reorganize things such that code can be reused without pain. Or less pain, you still have to avoids conflicts such that code blocks play along with each other. But there will be rules that you can blindly follow to successfully achieve that.

The logic of program will be the same, but using using functions means that the content of each box in the flow chart will be simpler. We just have to figure out how to call code (instead of write code) that does some job and then returns:

What are leaf functions?

In this session we will consider a special subset of functions, so called leaf functions. These are functions that by themself don't call other functions, so we only deal with a subcategory or subset of functions.

By the way

Working with functions will feed your desire to write more complex programs. This in turn will feed your desire to split a program into separate compilation units. Be patient and know that some day a linker will make things easier for you ;-)