thinkingincode.ninja [Dark]

Functions

Loops allow you to repeat a block of code multiple times, in one place, but what if you want to reuse a block of code in more than one location? That's where functions* come in. A function can be called (invoked) with different parameters, allowing it to change its behavior or perform the same operations against different data. Further, functions can return a value. A function has the following components: a name, a parameter list, and a function body. Most languages also require the return type to be specified.

Consider the following, a function which returns the maximum of two numbers passed in as parameter. Here, there is no keyword used to specify that a function is being declared and defined, instead the computer determines this based off of other parsing rules. The return type of the function is int, and two int parameters are defined. Within the body of the function, a and b refer to the two parameters that the function was called with. The keyword return is used to terminate the function, and give the specified value back to the place that called the function.

int GetMax(int a, int b) {
    if( a > b ) {
        return a;
    } else {
        return b;
    }
}

Functions are generally called or invoked by writing the name of the function, followed by a pair of parentheses ( ) with comma-separated parameters between the parentheses.

int num1 = 123;
int num2 = 92;
int num3 = 517;
int max = GetMax(num1, num2);
// max now has the value of 123
max = GetMax(max, num3);
// max now has the value of 517

// It can also be rewritten like this:
max = GetMax(GetMax(num1, num2), num3);
// First, GetMax(num1, num2) is called,
// then that value is provided as the first
// parameter to the second call of GetMax.

Functions are extremely useful, because they facilitate code re-use, and they allow programmers to abstract away certain implementation details. Proper naming of functions also makes it much easier for readers to understand a program, and can allow the reader to skip over reading a function implementation if the name is clear: the function called GetMax should be a function that returns the maximum value that is passed in as parameter; if it does not, then the function likely needs to be renamed.

Code re-use via functions is important because, among other reasons, if there's duplicate code throughout a program, it means that if you want to make a change to that code, you have to make sure that change is made to all instances of the duplication. If, on the other hand, a function is written that is called multiple times, then only that one function needs to be changed.

In many programming languages, program execution starts at a special function called main. When main exits, the program terminates. In other languages, program execution starts at the first statement in the code file being executed, and finishes when all statements have been executed.

*Other words for function are 'method', 'procedure', or 'subroutine'. Technically, referring to something as a 'method' vs a 'procedure' carries a slightly different connotation, but usually referring to something as a 'function' is sufficient.