This example is the same as the first graphing version but places the graphing code in a function called newgraph
.
Description and Discussion
In this particular case the use of a function
does not make a huge amount of difference. It makes the forever
loop more compact and a little easier to read. If all of the code from the previous examples were combined then the forever
loop would become unwieldy. If the code is difficult to understand and review then bugs are more likely to creep in.
Functions become more useful in a program when they are used (called) multiple times as they reduce code duplication. They can also be the first step towards sharing code between different programs.
Page last edited March 08, 2024
Text editor powered by tinymce.