Deconstructing functions

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.⁴

Most modern programming languages — Javascript is a classic example — often allow for predefined (or built-in) functions to be called. In such languages, a function can be assigned as a value to a variable, or can be passed into arrays (lists) just like any other data type.⁵ Such type of procedures is often employed in functional programming, a language design that mostly relies on functions, rather than variables.

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.

Javascript code behind TEXTBOX

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


References:

  1. BBC Bitesize — GCSE Computer Science — Functions, procedures and modules — Revision 1(2018). Available at: https://www.bbc.co.uk/education/guides/z9hykqt/revision.
  2. Programming — Functions (2018). Available at: https://www.cs.utah.edu/~germain/PPS/Topics/functions.html (Accessed: 19 January 2018).
  3. Sridharan, C. (2018) Small Functions considered Harmful, Medium. Available at: https://medium.com/@copyconstruct/small-functions-considered-harmful-91035d316c29.
  4. Derek Robinson (2008) Software studies by Fuller, M. Cambridge, Mass.: MIT Press.
  5. Goldberg, O. (2017) Javascript and Functional Programming — Pt.2 : First Class Functions, Hacker Noon — Medium. Available at: https://hackernoon.com/javascript-and-functional-programming-pt-2-first-class-functions-4437a1aec217.

Francesco Imola is a London-based musician, weekend photographer, and current Sound Design student at the University of Greenwich.

w. http://francescoimola.com/

tw. https://twitter.com/francescoimola