When thinking of code as a set of instructions is it useful to assign a name to those chunks of instructions. The two fundamental ones (in programming) are called procedures and functions.
Functions and procedures share many similarities — in some programming languages procedures are seen as a special type of functions — but generally speaking, functions return a value, whereas procedures don’t.¹
Functions are self-contained modules of code that accomplish a specific task.² The aim of a function is to “acquire” data, process it, and “return” a result (or value). To streamline and maintain modern code, functions are now usually limited to performing a single task and are kept as short as possible. [To some programmers], the idea that functions should be small is something that is almost considered too sacrosanct to call into question.³
The power of functions resides in their virtually infinite modularity. They allow programmers to shorten code, reusing it instead of rewriting it.² A function can be written once and used over and over again at different points within a programme. It is also possible to call functions from the inside of other functions. Some typical examples of functions would be arithmetic operators like “plus,” “times,” and “square root,” which can be combined with other arithmetic operations to compose expressions.⁴
To announce a function we firstly need to assign this a name (or identity) and declare any argument that it will require. When a function is called the programme “leaves” the current section of code and begins to execute the first line inside the function.² Once inside the function, the code is run from top to bottom. The resulting value is sent back to where the function was called in the code and is used in place of this.
Additionally, functions can be seen as look-up tables that map from a certain symbol, the look-up key, to a value associated with this key.⁴
A function eventually represents the simplest machine design. However, completely functional programs can only recreate an ideally logical data in-data out machine, not the one which we usually have to deal with. Every variable that lives inside the workspace of a function is only usable during the execution of the function. Good designs are to be based on a reversible logic — one which a function-only driven programme will not be able to accomplish.
This intuition is at the heart of logic. If repeating the same operation with the same input gives a different output, you know without a doubt that something changed: it isn’t the function you thought it was, it isn’t a simple machine.(Derek Robinson, 2008)
Implementing functions into my coding projects
For the past week I’ve been working on an extremely small and simplistic program that develops on the concept of text and type, and uses functions as main building blocks.
The project is called TEXTBOX. Its aim is to generate an artwork out of the text the user writes in the actual textbox when hitting the submit button. It is inspired by this example — which I have encountered while leaning more about using Input and Buttons on p5.
I’ve called the greet function through the mousePressed action/built-in function in p5. In greet is placed the for loop which generates the copy of the text, and changes its colour and position randomly. Simply reload the page to reset the background to white.
An nice update to this would be adding a refresh button inside the actual webpage, as well as the ability to save the artwork (with different aspect-ratios) as a picture on your computer.
The project can be accessed here https://alpha.editor.p5js.org/full/SJvi1DzrG
- BBC Bitesize — GCSE Computer Science — Functions, procedures and modules — Revision 1(2018). Available at: https://www.bbc.co.uk/education/guides/z9hykqt/revision.
- Programming — Functions (2018). Available at: https://www.cs.utah.edu/~germain/PPS/Topics/functions.html (Accessed: 19 January 2018).
- Sridharan, C. (2018) Small Functions considered Harmful, Medium. Available at: https://medium.com/@copyconstruct/small-functions-considered-harmful-91035d316c29.
- Derek Robinson (2008) Software studies by Fuller, M. Cambridge, Mass.: MIT Press.
Francesco Imola is a London-based musician, weekend photographer, and current Sound Design student at the University of Greenwich.