Learn Functions in programming

Codistwa

Learn Functions

in Programming

Reading time : 3 min

Last update : February 11th, 2022

Functions in programming allow having a cleaner code because they allow a structured program and avoid repetitions. They form a subprogram. They can be used several times in another part of the program, in another function, or the same function (recursive function). This is a way to test your code in a unitary way.

You can pass information to a function via what is called a parameter. Then, in the function's body, some tasks will be performed (for example, doubling the parameter), then return a result.

Finally, the function is executed by calling it with the parentheses. At this point, the information used during the function call is called argument.

There is a style of programming called functional programming whose goal is to use only functions to structure the program. It is more or less in opposition with object-oriented programming.

Half off everything 2manning

Without parameter

We start with a simple function that has no parameters. We call the function to see the result.

The machine

Code

javascriptjavascript
1function double () {
2  console.log('Hello world')
3}
4
5console.log(double()) // Hello world

One parameter

Now a function that has only one parameter and returns a value.

Twins

Code

javascriptjavascript
1function double (x) {
2  return x * 2;
3}
4
5const square = (x) => x ** 2;
6
7console.log(double(4)); // 8
8console.log(square(4)); // 16

Two parameters

Finally, a function that has two parameters. You can add as many parameters as you want, but for the sake of the readability of the code, it is recommended to use three parameters maximum. If you have to add more parameters, it means that you can cut the function in two. Also, it is better to avoid functions with too many lines of code.

Code

javascriptjavascript
1const ids = (firstName, lastName) => 
2  `My name is ${firstName} ${lastName}`;
3
4console.log(ids('Mickael', 'Jackson'));

Scope

The notion of scope is essential because a function can access a global variable and its local variable(s). But, on the other hand, it does not have access to the variables of other functions.

Code

javascriptjavascript
1const apple = 3;
2
3const getApple = (apple) => apple - 2;
4
5console.log(getApple(30)); // 28

Optional, default

You can define default parameters directly; this allows you to have clean and readable code. So, just by looking at the parameters, you know what it expects. You don't have to define it in the body of the function. Parameters can be optional and return a specific code if not used during the call, like undefined in JavaScript. It is, however, advisable to define a default one like an empty string '' for example.

Code

javascriptjavascript
1const id2 = (firstName, age = 30) => {
2  if (firstName) {
3    return `My name is ${firstName} and I have ${age} years old`;
4  } 
5  return `I have ${age} years old`;
6}
7
8console.log(id2('Francis')); // My name is Francis and I have 30 years old
9console.log(id2()); // I have 30 years old
Print booksmanning

Functions are handy in programming because they allow you to divide the program into subprograms, and thus the code is more readable. Parameters are defined when the function is created and then arguments when it is called. A function must return a value. It performs the tasks in its body. It has a scope that allows the managing of global and local variables.

Ressources

Source code