Some reflections on functions

Another trip into the past and back to the future

Thomas Künneth
Tommis Programming Blog
3 min readJan 11, 2020

--

A small BASIC program running on a C64 emulator
Subroutines in BASIC

Confession: I am still (that is, more than 35 years later) fascinated by the home computers of the early 80s. Compared to the capabilities of todays’ computers those tiny machines seem insanely slow and basic. Still it was very inspiring and enlightening to program them. Yes, program, not play. :-) Join me on yet another brief journey into the past. And, of course, a look at how things work today.

Let’s take a look at the following small program:

10 PRINT ”HELLO”;
20 GOSUB 70
30 FOR I = 1 TO 3
40 GOSUB 70
50 NEXT
60 END
70 PRINT ” WORLD”
80 RETURN

In early BASIC dialects you reuse code through so-called subroutines. They are invoked using the GOSUB keyword. The destination is mentioned through a line number. Once called, program execution remains in the subroutine until a RETURN is found, or the program flow is altered otherwise. Please note: you cannot pass arguments to the subroutine. And you cannot return values. To achieve this one would use (global) variables. This may seem pretty odd. Still, subroutines are not functions. You will see shortly what I mean by this. Before, two additional explanations:

  • END marks the end of the program. Although in some cases END can be omitted, it is good practice to put it before the first line number of the first subroutine because otherwise the program might enter the code without an explicit GOSUB.
  • Subroutines can be called from different parts of the program

Now let’s turn to real functions.

10 DEF FN POW2(X) = X * X
20 INPUT “PLEASE ENTER A NUMBER: “; A
30 PRINT FN POW2(A)

DEF FN defines a function, whereas FN invokes it.

A BASIC program showing DEF FN
How to define a function in BASIC

That was fun, wasn’t it? I bet even older ones did not remember DEF FN. :-) Now back to the future… JavaScript has the function keyword. It is used as follows:

function pow2(x)
{
return x * x;
}

There is a shorter way using the arrow:

let pow2 = (x) => x*x;

To invoke both versions use something like pow2(3). Swift is similar, but uses the func keyword:

func pow2(x: Double) -> Double {
return x * x;
}

We invoke it like this:

print(pow2(x: 3.0))

Dart can be very terse:

pow2(x) => x * x;

Or more descriptive:

double pow2(double x) {
return x * x;
}

Finally, Scheme:

(define pow2 (lambda (x) (* x x)))
(pow2 3)

What other language feature would you like to see compared? Feel free to suggest your favorites…

A previous (less complete) version of this article was published 2017–05–08 on my Blogger.com blog Tommis Blog. I deleted the weblog in the wake of the GDPR (General Data Protection Regulation), so the original post is no longer available (or only through the Wayback machine of the Internet Archive).

If not stated otherwise images are © Thomas Künneth

--

--

Thomas Künneth
Tommis Programming Blog

Google Developer Expert for Android. Author. Speaker. Listener. Loves writing.