88
4 Functions and the Writing of Code
Remember that only one of the branches is executed for a single call to
check_sign, so depending on the number x, the return may take place from
any of the three return alternatives.
To Return at the End or Not
Programmers disagree whether it is a good idea to use return inside a
function, or if there should be just a single return statement at the end of
the function. The authors of this book emphasize readable code and think that
return can be useful in branches as in the example above when the function
is short. For longer or more complicated functions, it might be better to have
one single return statement at the end.
Nested Functions
Functions may also be defined within other functions. In that case, they
become local functions, or nested functions, known only to the function inside
which they are defined.
Functions defined in main are referred to as global functions. A nested
function has full access to all variables in the parent function, i.e. the function
within which it is defined.
Overhead of Function Calls
Function calls have the downside of slowing down program execution.
Usually, it is a good thing to split a program into functions, but in very
computing intensive parts, e.g., inside long loops, one must balance the
convenience of calling a function and the computational efficiency of avoiding
function calls. It is a good rule to develop a program using plenty of functions
and then in a later optimization stage, when everything computes correctly,
remove function calls that are quantified to slow down the code. Let it be clear,
however, that newcomers to programming should focus on writing readable
code, not fast code!
In Sect. 5.6, we investigate (for a particular case) the impact that function
calls have on CPU time.
4.2 Programming as a Step-Wise Strategy
When students start out with programming, they usually pick up the basic ideas
quite fast and learn to read simpler code rather swiftly. However, when it comes to
the writing of code, many find it hard to even get started.
4 Functions and the Writing of Code
Remember that only one of the branches is executed for a single call to
check_sign, so depending on the number x, the return may take place from
any of the three return alternatives.
To Return at the End or Not
Programmers disagree whether it is a good idea to use return inside a
function, or if there should be just a single return statement at the end of
the function. The authors of this book emphasize readable code and think that
return can be useful in branches as in the example above when the function
is short. For longer or more complicated functions, it might be better to have
one single return statement at the end.
Nested Functions
Functions may also be defined within other functions. In that case, they
become local functions, or nested functions, known only to the function inside
which they are defined.
Functions defined in main are referred to as global functions. A nested
function has full access to all variables in the parent function, i.e. the function
within which it is defined.
Overhead of Function Calls
Function calls have the downside of slowing down program execution.
Usually, it is a good thing to split a program into functions, but in very
computing intensive parts, e.g., inside long loops, one must balance the
convenience of calling a function and the computational efficiency of avoiding
function calls. It is a good rule to develop a program using plenty of functions
and then in a later optimization stage, when everything computes correctly,
remove function calls that are quantified to slow down the code. Let it be clear,
however, that newcomers to programming should focus on writing readable
code, not fast code!
In Sect. 5.6, we investigate (for a particular case) the impact that function
calls have on CPU time.
4.2 Programming as a Step-Wise Strategy
When students start out with programming, they usually pick up the basic ideas
quite fast and learn to read simpler code rather swiftly. However, when it comes to
the writing of code, many find it hard to even get started.
