4 Years
Discussion Span
Last Post by rubberman

yes. For small programs like you might write in first year programming courses it doesn't really matter a whole lot, unless of course you are learning how to write functions. They become very important in larger programs to keep the functions small and understandable.

Edited by Ancient Dragon


Does the number of functions in a program affect performance?

It can. There's a certain measure of overhead in calling a function at runtime.

Is writing a lot of functions in a program a good practice?

Not if there's no reason for writing "a lot" of functions. Programs should be structured in a way that makes them as readable as possible without negatively affecting performance.


In any programme of sufficient size and complexity you're likely to have a large number of functions. Quite often an organisation's coding standards provide guidelines on the length of functions, limiting their size. For example you might have a 60-line limit as a guide. It's quite normal to have lots of functions.

Most of the time you don't need to worry about the effect on performance because the overhead of function calls is small, but in some environments you might need to optimise.


As said,the overhead for function calling is negligible.And of course,that can be avoided too by declaring small functions(Small here is relative) inline.That is likely to generate a lot of object code,but is said to be faster.


That is likely to generate a lot of object code,but is said to be faster.

Until the added object code size causes excess cache misses. ;) Despite the common belief, inline functions aren't guaranteed to improve anything, and could just as easily make things worse or have no measurable effect.


The overhead of function calling is directly proportionate to the number of arguments and automatic variables declared in the function. They all have to be pushed onto the stack. Also, deep recursion will cause similar overhead. That said, you are generally better off to have a bunch of small, easily understood functions rather than just a few big/complex ones. Remember the KISS principal. It is pre-eminent in software engineering.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.