Page of Alena's post with the title Function keyword doesn't mean it's a function

⭐️ 💖

Back to home page

Alena Nikolaeva

Germany

Frontend Developer

Function keyword doesn't mean it's a function

Updated: 18.08.2020

7 min

Identifying Functions

function name([param[, param[, ... param]]]) {
   statements
}

"...You can't do functional programming with something that is not a function..."

We all can name basic principles of functional paradigm as well as what functional programming isn't. Pure functions, avoiding shared state, mutable data, side-effects, recursion. There a tons of good material on the topic out there.

Functional programming requires us to write pure, deterministic functions. Whether, it's pure or not, how do you even know what a function is?



"...A function is a specific type of relation in which each input value has one and only one computed output value. An input is the independent value, and the output value is the dependent value, as it depends on the value of the input...."

In human:

  • Function has to have a return keyword
  • Presence of obvious relationship between input and output (notice that undefined is a valid output, the absence of the input is a valid input).
  • The inputs and the outputs has to be direct.
  • Each input has one and only one output value

If one of the condition is not applicable, we are not talking about function, we are talking about procedure. Both are small sections of code that are used to perform a particular task. But function returns a value, while a procedure doesn't.

You can describe procedure, as well, as a set of commands which takes input and performs certain task in order.

  • In SQL, procedure does not return a value.
  • In java, procedure and functions are same and also called sub-routines.
  • In Pascal, procedures don't return values and functions return values.

Functional programming treats computation as the evaluation of mathematical functions. In the 8th Grade they teach you how to identify the function.

To identify a function, first we identify relation. Consider members of your family and their favorite color as an example. Each family member can be paired with favorite color in the set of your family members. This simple example can be called function, where the input is “name” and the output is favorite color, each input matches with exactly one output. If two persons like exactly the same color (the input is still favorite color and the output is name, result is the same in more than one output. This is an example of a correspondence that is not a function.

A function is a specific type of relation in which each input value has one and only one output value. A relation is simply a relationship between two sets of numbers or data.

A function is particular case of relationship between two sets with specific conditions. Let's say our family members is set A and their favorite colors is set B. For each of the elements of A, there is only one element of B that is related to A. ∀a ∈ A, ∃! b ∈ B / afb

Some of the most common ways to represent functions include: sets of ordered pairs, equations, and graphs.

In math:

f(x) = 2x + 1

JavaScript equivalent:

const myFunction = x => x * 2 + 1;

Going back to the function definition in programming languages, we can now identify a function.

  • return keyword
  • it must return the same value always given a specific input

Returns a value mean that the function creates some sort of results, which is passed back to the calling function.

Function:

int myFunction( int n ) {
   return n * n; // returns value
}
Function Celsius(fDegrees)
    _Celsius = (fDegrees - 32) * 5 / 9
    return _Celsius;
function multiply(a, b) {
  return a * b;
}

Procedure:

void display( int n ) {
   printf( "The value is %d", n ); // returns void (nothing)
}
import time

print (time.strftime("%H:%M:%S")) #strftime is a proccedure in the time module.
def procedureName(arg1, arg2, ...):
    print('put instructions here')
    
procedureName()

function multiply(a, b) {
  console.log('Arguments: ', a, b);
}

A method that has a return keyword, but it's not a function.

result(x) = {
  if (otherFunction()) {
     result = x * 2;
  }
  else {
   result = x;
  }
  return result;
}

The term "procedural programming" refers to a whole class of languages, including BASIC, C, FORTRAN, Java, and Pascal. A procedure can cause side effects or make arguments do something.

In JavaScript, a function allows you to define a block of code, give it a name and then execute it. Functional programming distinguishes between pure and impure functions. In order to understand the power of functional programming in Javascript, we need to know what function is and once we have identified a function, it has a return key word and the returned values are entirely determined by the inputs, and the function doesn't have any side effects, then it's called a pure function.

Now we are closer to the first fundamental concept and its benefits: understand pure functions.

References