What’s the point of Functions in #programming? (for primary/middle schoolers)

My common day-to-day use is “something that is used to generate individual parts of something, and that I might write alternative versions of in future”.

Here the use of a function lets you replace a generator with a different generator. Such examples occur frequently in OOP, but are only truly dependent upon functions.

For instance:

  1. Draw a graph on screen (using turtle in python, or drawing rectangles at heights – anything will do)
  2. At first, have it draw y = 0. Then draw y=x.
    Replace the bit that chooses y with a call to a function.

  3. Have the students write their own functions, and replacing only one line of code to get massively different effects
  4. If your language supports it, you can have the user type the name of a function, and it will graph that function

…the point being: it allows modularised code that returns a value which is then used repeatedly with different parameters within a larger program, without the two bits of code being coupled together.

My favourite “fancy” example (for which I have extensive video clips and actual code I’ve written and used in production) is in 3D. Here I draw 3D terrains using a function to provide a Z height for each X,Y co-ordinate:

Leave a Reply