Gene Gajewski

Personal Blog

Close this search box.

You do not need to be a musician to simply listen to and appreciate music! Really! But: Why is that so?

Well, it just happens that the answer to that question is related to one of the C++ languages features I am using in a program that I am developing for my Ham Radio hobby. You see, even if I have one hobby on hiatus (music in this case) because I am focusing on another, I am still thinking about the former. Our World and our Hobbies are way more inter-related than you think.

Music. You recognize it, you sense it, it all together makes sense. It can affect your mood in ways not fully understood by some. To break it down: Your brain can tell if two sounds have the same fundamental tone. It can tell if a sound is twice or half its fundamental tone and it can also sense the difference between two tones, or even three or more. It senses the relationship between those tones as well. Your brain senses the volume of a tone and can sense the timing (rhythm) between tones. Quite remarkable, and especially so if you do not know a thing about music. So, what do we call this capability of ours, this ability to distinguish?

A mathematician would call this ability a function. In the expression

a * b

the * represents the multiplication function, which represents the result of its arguments multiplied together. Other mathematical operators are functions as well: +, -, / all are operators on their arguments, and all give a known result. But what I described earlier are nameless (to the untrained) musical functions. Truly though, the notion of a function is much simpler: Webster defines a function as “a variable (such as a quality, trait, or measurement) that depends on and varies with another”.

We all have these musical functions in our minds, and they behave much the same as mathematical functions, they can compare things, they measure something. The problem is, for the non-musically trained, is what to call these functions?

Simply put, the musicians call the fundamental of a tone its pitch (frequency), and the characteristic sound of a tone its timbre. Timbre is the ability to distinguish two tones of the same pitch, but of a different characteristic sound, such as a saxophone vs. a trumpet. The big point that I am making here is that the musically trained have given names to these musical functions of the mind that we all have. For the non-musically trained, we still have (and use) these musical functions, we just have not recognized them as functions and thus have not given them a name!

A function with no name (sounds like the title for a song…)

A function with no name is exactly what I am getting at, even if I have taken the long route here to get to the point. In C++, a function with no name is known as an anonymous function, or lambda function (a Greek nod to a function’s mathematical usage). Believe it or not, it has only been just over a little over 10 years as I write this post since popular computer programming languages have finally adopted the lambda function and thus recognize that the utility of a function (what it does) is separate from its name (what you call it). C/C++ and other computer programming languages have always, in the past, required that the functions you define have names. An anonymous function is the equivalent of an internet sock-puppet: you don’t know who or what it is, but you sure can tell that it is there by observing what it’s doing. Heh.

This anonymous function surprise! (to an old-school programmer’s thinking) is because functions to them are simply procedures that also happen to return a result, and procedures always have names. They have names because we need to know what to call them if we refer to them later in our programs. How can we use a function if we do not know what to call it? (How can you have any pudding if you don’t eat your meat?) Brain hurts….

Well, if we can mentally process music with our mind’s intrinsic nameless functions, we ought to be able to do that in a computer programming language as well. The lambda function in C++ allows you to declare an anonymous function in-situ and pass it as an argument to another function. The called function receives the nameless function as a parameter. This operates much the same as a C function pointer being passed as an argument. However, the lambda bests even the mighty function pointer. The lambda function can modify program variables in the context of the calling code – this is call capturing context. A regular C function can only modify variables from the calling context through specific arguments passed as pointers or references. Anyway, I’ll stop here as we’re getting a more than a bit deep into a discussion on a programming language’s syntactic sugar.

One last thought though: A program procedure should really be considered as a function as well, even if it does not return a value because, what is does is separate from what it is called, and just like a function, it only needs a name if we give it one.

Leave a Reply

Share the Post: