Programming Functions and why are they so special

806 views

I’m still trying to wrap my ahead around this concept.

I’ve done basic maths, so I know that f(x) = x is like a function, but I can’t find an intuitive way of explaining why some functions don’t have to return values. In addition, what separates functions from just lines of code?

In: Technology

12 Answers

Anonymous 0 Comments

Technically, all programming functions has outputs, but in some cases we don’t see them:

1. The function requires multiple outputs but the programming language does not support functions with multiple outputs. In this case we get around the problem by using pointer inputs to get their physical addresses.
2. It create an output(s) that is either physical or an essential component in your framework or library. They are either fixed or declared so you can’t define them.
3. Also in object oriented programming you can call “methods” of an object to change its properties. Since properties of the class of the object can be directly accessed by the method there’s no need of returning an output. This is really meaningful because it allow encapsulation.

Anonymous 0 Comments

The word “function” has slightly different meaning in programming vs. math.

In math, a function is a set of ordered pairs. Meaning a bunch of `(x, y)` values. Each particular `x` value (in the “domain”) occurs exactly once. In math, a function is often expressed as a formula for turning the `x` value into the `y` value. The notation `f(5)` means “the `y` part of the ordered pair for which the `x` part is `5`”. But in math, the technical definitions for what exactly is meant by the word “function” isn’t the formula. It’s the set of ordered pairs. In higher theoretical math, there’s ways to build functions that you can’t write down formulas for, instead you make a detailed technical description of a set of ordered pairs (and often when you do this, you also have to thoroughly explain why an object matching your description must exist, even though there’s no way for you to write down a formula for it).

In programming, a function is a procedure. It’s a list of steps for the computer to do. That list of steps may take some input value and produce an output value, so you can still have for example `f(5)` that applies some formula to the number `5`. But in programming, unlike in math, there is no way to write a function without writing code. The function *is* the code.

The other difference in programming is that some of the steps you can do have *side effects*. One example of a side effect is writing some information to a “global” variable, which changes how other code runs. Another example is drawing some pixels or text on the screen. Saving stuff to the disk. Playing sound through the speakers.

In math, all the “side effects” of a function have to be captured in how you define the inputs and outputs. In most programming languages, *any* function can take extra inputs other than its arguments, and have extra outputs other than its return value. Those extra inputs / outputs are stuff like global variables, disk files, network stuff, OS events, user actions, etc. (Of course you *can* use a math function to describe the action of a computer language, you just have to make the entire computer’s current state part of the input of every step, and make its new state part of the output to every step. Yes, you can still do math with functions involving an entire computer memory, not just single numbers. But this isn’t usually a topic you study until junior / senior year of university.)

Yes, this can get really confusing really quickly. Which is why many programming design guidelines discourage unnecessarily choosing designs based on side effects, and separate/document any code involving side effects. But it’s a balancing act, because ultimately most programs have to have *some* side effects in order to be useful — you have to interact with the user, or at least somehow get information into / out of the program.

In practical code, a function that doesn’t return a value almost always has some side effects. For the good and simple reason that if your code doesn’t return a value, *and* doesn’t have any side effects…it literally does nothing!

> what separates functions from just lines of code?

A function (usually) has a name, and it is callable as a separate piece of code. So you can re-use it from different multiple places. You could think of functions as roughly equivalent to being a kind of short-hand, a way to tell the computer to copy-paste all the lines of code from where the function’s defined to where it’s used and substitute the arguments. (In fact this kind of copy-pasting has a name, it’s called “inlining” a function.) I say “roughly” equivalent, because there are (somewhat mind-bending) cases involving *recursion*, where a function calls *itself* either directly or indirectly, where the mental model of “function = shorthand for inlined code” breaks down.