What Is The Optimum Size For Functions?

Generally speaking, the source code for functions should be kept small, as this aids in readability and debugging. It is much easier to describe and debug the operation of a function that performs a small number of tasks. And small functions typically have fewer side effects, which can be the source of coding errors. In the embedded programming world, however, a large number of small functions, as well as the calls necessary to execute them, can result in excessive memory and stack usage, so a compromise is often necessary.

The PIC10/12/16 devices employ pages in the program memory that are used to store and execute function code. Although you are able to write C functions that will generate more than one page of assembly code, functions of such a size should be avoided and split into smaller routines where possible. (see Allocation of Executable Code) The assembly call and jump sequences to locations in other pages are much longer than those made to destinations in the same page. If a function is so large as to cross a page boundary, then loops (or other code constructs that require jumps within that function) can use the longer form of jump on each iteration.

PIC18 devices are less affected by internal memory paging and the instruction set allows for calls and jumps to any destination with no penalty. But you should still endeavor to keep functions as small as possible.

Interrupt functions must be written so that they do not exceed the size of a memory page. They cannot be split to occupy more than one page.

With all devices, the smaller the function, the easier it is for the linker to allocate it to memory without errors.