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.

This guide was first published on Sep 17, 2018. It was last updated on Sep 17, 2018.

This page (New Graph v2) was last updated on Sep 05, 2020.

Text editor powered by tinymce.